mmap.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * _mmap() for uClibc
  4. *
  5. * Copyright (C) 2000-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * GNU Library General Public License (LGPL) version 2 or later.
  8. */
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <sys/mman.h>
  12. #include <sys/syscall.h>
  13. #if defined (__NR_mmap) || defined (__NR_mmap2)
  14. libc_hidden_proto(mmap)
  15. #if defined (__UCLIBC_MMAP_HAS_6_ARGS__) && defined (__NR_mmap)
  16. #define __NR__mmap __NR_mmap
  17. static __inline__ _syscall6 (__ptr_t, _mmap, __ptr_t, addr, size_t, len,
  18. int, prot, int, flags, int, fd, __off_t, offset)
  19. __ptr_t mmap(__ptr_t addr, size_t len, int prot,
  20. int flags, int fd, __off_t offset)
  21. {
  22. return (__ptr_t) _mmap (addr, len, prot, flags,
  23. fd, offset);
  24. }
  25. #elif defined (__NR_mmap2)
  26. #define __NR__mmap __NR_mmap2
  27. #ifndef MMAP2_PAGE_SHIFT
  28. # define MMAP2_PAGE_SHIFT 12
  29. #endif
  30. static __inline__ _syscall6 (__ptr_t, _mmap, __ptr_t, addr, size_t, len,
  31. int, prot, int, flags, int, fd, __off_t, offset);
  32. __ptr_t mmap(__ptr_t addr, size_t len, int prot,
  33. int flags, int fd, __off_t offset)
  34. {
  35. /* check if offset is page aligned */
  36. if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1))
  37. {
  38. __set_errno(EINVAL);
  39. return MAP_FAILED;
  40. }
  41. #ifdef __USE_FILE_OFFSET64
  42. return (__ptr_t) _mmap (addr, len, prot, flags,
  43. fd, ((__u_quad_t) offset >> MMAP2_PAGE_SHIFT));
  44. #else
  45. return (__ptr_t) _mmap (addr, len, prot, flags,
  46. fd, ((__u_long) offset >> MMAP2_PAGE_SHIFT));
  47. #endif
  48. }
  49. #elif defined (__NR_mmap)
  50. # define __NR__mmap __NR_mmap
  51. static __inline__ _syscall1(__ptr_t, _mmap, unsigned long *, buffer)
  52. __ptr_t mmap(__ptr_t addr, size_t len, int prot,
  53. int flags, int fd, __off_t offset)
  54. {
  55. unsigned long buffer[6];
  56. buffer[0] = (unsigned long) addr;
  57. buffer[1] = (unsigned long) len;
  58. buffer[2] = (unsigned long) prot;
  59. buffer[3] = (unsigned long) flags;
  60. buffer[4] = (unsigned long) fd;
  61. buffer[5] = (unsigned long) offset;
  62. return (__ptr_t) _mmap(buffer);
  63. }
  64. #endif
  65. libc_hidden_def (mmap)
  66. #else
  67. # error "Your architecture doesn't seem to provide mmap() !?"
  68. #endif