Back to index

glibc  2.9
mmap64.c
Go to the documentation of this file.
00001 /* Copyright (C) 1999,2000,2001,2002,2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 1999.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <unistd.h>
00022 #include <sys/mman.h>
00023 
00024 #include <sysdep.h>
00025 #include <sys/syscall.h>
00026 #include <bp-checks.h>
00027 
00028 #include <kernel-features.h>
00029 
00030 #ifdef __NR_mmap2
00031 
00032 /* This is always 12, even on architectures where PAGE_SHIFT != 12.  */
00033 # ifndef MMAP2_PAGE_SHIFT
00034 #  define MMAP2_PAGE_SHIFT 12
00035 # endif
00036 
00037 # ifndef __ASSUME_MMAP2_SYSCALL
00038 static int have_no_mmap2;
00039 # endif
00040 #endif
00041 
00042 
00043 void *
00044 __mmap64 (void *addr, size_t len, int prot, int flags, int fd, off64_t offset)
00045 {
00046 #ifdef __NR_mmap2
00047   if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1))
00048     {
00049       __set_errno (EINVAL);
00050       return MAP_FAILED;
00051     }
00052 # ifndef __ASSUME_MMAP2_SYSCALL
00053   if (! have_no_mmap2)
00054 # endif
00055     {
00056 # ifndef __ASSUME_MMAP2_SYSCALL
00057       int saved_errno = errno;
00058 # endif
00059       void *result;
00060       __ptrvalue (result) = (void *__unbounded)
00061        INLINE_SYSCALL (mmap2, 6, __ptrvalue (addr),
00062                      len, prot, flags, fd,
00063                      (off_t) (offset >> MMAP2_PAGE_SHIFT));
00064 # if __BOUNDED_POINTERS__
00065       __ptrlow (result) = __ptrvalue (result);
00066       __ptrhigh (result) = __ptrvalue (result) + len;
00067 # endif
00068 # ifndef __ASSUME_MMAP2_SYSCALL
00069       if (result != MAP_FAILED || errno != ENOSYS)
00070 # endif
00071        return result;
00072 
00073 # ifndef __ASSUME_MMAP2_SYSCALL
00074       __set_errno (saved_errno);
00075       have_no_mmap2 = 1;
00076 # endif
00077     }
00078 #endif
00079 #ifndef __ASSUME_MMAP2_SYSCALL
00080   if (offset != (off_t) offset || (offset + len) != (off_t) (offset + len))
00081     {
00082       __set_errno (EINVAL);
00083       return MAP_FAILED;
00084     }
00085 
00086   return __mmap (addr, len, prot, flags, fd, (off_t) offset);
00087 #endif
00088 }
00089 weak_alias (__mmap64, mmap64)