mmap.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/syscall.h>
  10. #include <unistd.h>
  11. #include <sys/mman.h>
  12. #include <bits/uClibc_page.h>
  13. #ifdef __NR_mmap
  14. libc_hidden_proto(mmap)
  15. #ifdef __UCLIBC_MMAP_HAS_6_ARGS__
  16. _syscall6(void *, mmap, void *, start, size_t, length,
  17. int, prot, int, flags, int, fd, off_t, offset);
  18. #else
  19. # define __NR__mmap __NR_mmap
  20. static inline _syscall1(__ptr_t, _mmap, unsigned long *, buffer);
  21. __ptr_t mmap(__ptr_t addr, size_t len, int prot,
  22. int flags, int fd, __off_t offset)
  23. {
  24. unsigned long buffer[6];
  25. buffer[0] = (unsigned long) addr;
  26. buffer[1] = (unsigned long) len;
  27. buffer[2] = (unsigned long) prot;
  28. buffer[3] = (unsigned long) flags;
  29. buffer[4] = (unsigned long) fd;
  30. buffer[5] = (unsigned long) offset;
  31. return (__ptr_t) _mmap(buffer);
  32. }
  33. #endif
  34. libc_hidden_def(mmap)
  35. #elif defined(__NR_mmap2)
  36. libc_hidden_proto(mmap)
  37. #define __NR___syscall_mmap2 __NR_mmap2
  38. static inline _syscall6(__ptr_t, __syscall_mmap2, __ptr_t, addr,
  39. size_t, len, int, prot, int, flags, int, fd, off_t, offset);
  40. /* Some architectures always use 12 as page shift for mmap2() eventhough the
  41. * real PAGE_SHIFT != 12. Other architectures use the same value as
  42. * PAGE_SHIFT...
  43. */
  44. # ifndef MMAP2_PAGE_SHIFT
  45. # define MMAP2_PAGE_SHIFT 12
  46. # endif
  47. __ptr_t mmap(__ptr_t addr, size_t len, int prot, int flags, int fd, __off_t offset)
  48. {
  49. if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1)) {
  50. __set_errno(EINVAL);
  51. return MAP_FAILED;
  52. }
  53. return __syscall_mmap2(addr, len, prot, flags, fd, offset >> MMAP2_PAGE_SHIFT);
  54. }
  55. libc_hidden_def(mmap)
  56. #endif