1/* mmap - map files or devices into memory. Linux version.
2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 1999.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <errno.h>
21#include <unistd.h>
22#include <sys/mman.h>
23#include <sysdep.h>
24#include <mmap_internal.h>
25
26#ifdef __NR_mmap2
27/* To avoid silent truncation of offset when using mmap2, do not accept
28 offset larger than 1 << (page_shift + off_t bits). For archictures with
29 32 bits off_t and page size of 4096 it would be 1^44. */
30# define MMAP_OFF_HIGH_MASK \
31 ((-(MMAP2_PAGE_UNIT << 1) << (8 * sizeof (off_t) - 1)))
32#else
33/* Some ABIs might use __NR_mmap while having sizeof (off_t) smaller than
34 sizeof (off64_t) (currently only MIPS64n32). For this case just set
35 zero the higher bits so mmap with large offset does not fail. */
36# define MMAP_OFF_HIGH_MASK 0x0
37#endif
38
39#define MMAP_OFF_MASK (MMAP_OFF_HIGH_MASK | MMAP_OFF_LOW_MASK)
40
41/* An architecture may override this. */
42#ifndef MMAP_PREPARE
43# define MMAP_PREPARE(addr, len, prot, flags, fd, offset)
44#endif
45
46void *
47__mmap64 (void *addr, size_t len, int prot, int flags, int fd, off64_t offset)
48{
49 MMAP_CHECK_PAGE_UNIT ();
50
51 if (offset & MMAP_OFF_MASK)
52 return (void *) INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
53
54 MMAP_PREPARE (addr, len, prot, flags, fd, offset);
55#ifdef __NR_mmap2
56 return (void *) MMAP_CALL (mmap2, addr, len, prot, flags, fd,
57 (off_t) (offset / MMAP2_PAGE_UNIT));
58#else
59 return (void *) MMAP_CALL (mmap, addr, len, prot, flags, fd, offset);
60#endif
61}
62weak_alias (__mmap64, mmap64)
63libc_hidden_def (__mmap64)
64
65#ifdef __OFF_T_MATCHES_OFF64_T
66weak_alias (__mmap64, mmap)
67weak_alias (__mmap64, __mmap)
68libc_hidden_def (__mmap)
69#endif
70