mmap.c 2.0 KB

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