mmap.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * mmap() for uClibc
  4. *
  5. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  6. *
  7. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  8. */
  9. #include <sys/mman.h>
  10. #include <sys/syscall.h>
  11. #if defined __UCLIBC_MMAP_HAS_6_ARGS__ && defined __NR_mmap
  12. # ifndef _syscall6
  13. # error disable __UCLIBC_MMAP_HAS_6_ARGS__ for this arch
  14. # endif
  15. # define __NR__mmap __NR_mmap
  16. static _syscall6(void *, _mmap, void *, addr, size_t, len,
  17. int, prot, int, flags, int, fd, __off_t, offset)
  18. #elif defined __NR_mmap2 && defined _syscall6
  19. # include <errno.h>
  20. # include <bits/uClibc_page.h>
  21. # ifndef MMAP2_PAGE_SHIFT
  22. # define MMAP2_PAGE_SHIFT 12
  23. # endif
  24. # define __NR___syscall_mmap2 __NR_mmap2
  25. static __inline__ _syscall6(void *, __syscall_mmap2, void *, addr, size_t, len,
  26. int, prot, int, flags, int, fd, __off_t, offset)
  27. static void *_mmap(void *addr, size_t len, int prot, int flags,
  28. int fd, __off_t offset)
  29. {
  30. const int mmap2_shift = MMAP2_PAGE_SHIFT;
  31. const __off_t mmap2_mask = ((__off_t) 1 << MMAP2_PAGE_SHIFT) - 1;
  32. /* check if offset is page aligned */
  33. if (offset & mmap2_mask) {
  34. __set_errno(EINVAL);
  35. return MAP_FAILED;
  36. }
  37. # ifdef __USE_FILE_OFFSET64
  38. return __syscall_mmap2(addr, len, prot, flags, fd,
  39. ((__u_quad_t) offset >> mmap2_shift));
  40. # else
  41. return __syscall_mmap2(addr, len, prot, flags, fd,
  42. ((__u_long) offset >> mmap2_shift));
  43. # endif
  44. }
  45. #elif defined __NR_mmap
  46. # define __NR___syscall_mmap __NR_mmap
  47. static __inline__ _syscall1(void *, __syscall_mmap, unsigned long *, buffer)
  48. static void *_mmap(void *addr, size_t len, int prot, int flags,
  49. int fd, __off_t offset)
  50. {
  51. unsigned long buffer[6];
  52. buffer[0] = (unsigned long) addr;
  53. buffer[1] = (unsigned long) len;
  54. buffer[2] = (unsigned long) prot;
  55. buffer[3] = (unsigned long) flags;
  56. buffer[4] = (unsigned long) fd;
  57. buffer[5] = (unsigned long) offset;
  58. return __syscall_mmap(buffer);
  59. }
  60. #else
  61. # error "Your architecture doesn't seem to provide mmap() !?"
  62. #endif
  63. strong_alias(_mmap,mmap)
  64. libc_hidden_def(mmap)