1 /* Copyright (C) 1994-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <sys/types.h>
24 /* Map addresses starting near ADDR and extending for LEN bytes. from
25 OFFSET into the file FD describes according to PROT and FLAGS. If ADDR
26 is nonzero, it is the desired mapping address. If the MAP_FIXED bit is
27 set in FLAGS, the mapping will be at ADDR exactly (which must be
28 page-aligned); otherwise the system chooses a convenient nearby address.
29 The return value is the actual mapping address chosen or (void *) -1
30 for errors (in which case `errno' is set). A successful `mmap' call
31 deallocates any previous mapping for the affected region. */
34 __mmap (void *addr
, size_t len
, int prot
, int flags
, int fd
, off_t offset
)
37 vm_prot_t vmprot
, max_vmprot
;
38 memory_object_t memobj
;
42 mapaddr
= (vm_address_t
) addr
;
44 /* ADDR and OFFSET must be page-aligned. */
45 if ((mapaddr
& (__vm_page_size
- 1)) || (offset
& (__vm_page_size
- 1)))
46 return (void *) (long int) __hurd_fail (EINVAL
);
48 vmprot
= VM_PROT_NONE
;
50 vmprot
|= VM_PROT_READ
;
51 if (prot
& PROT_WRITE
)
52 vmprot
|= VM_PROT_WRITE
;
54 vmprot
|= VM_PROT_EXECUTE
;
56 copy
= ! (flags
& MAP_SHARED
);
58 switch (flags
& MAP_TYPE
)
61 return (void *) (long int) __hurd_fail (EINVAL
);
64 memobj
= MACH_PORT_NULL
;
65 max_vmprot
= VM_PROT_ALL
;
69 case 0: /* Allow, e.g., just MAP_SHARED. */
71 mach_port_t robj
, wobj
;
72 if (err
= HURD_DPORT_USE (fd
, __io_map (port
, &robj
, &wobj
)))
74 if (err
== MIG_BAD_ID
|| err
== EOPNOTSUPP
|| err
== ENOSYS
)
75 err
= ENODEV
; /* File descriptor doesn't support mmap. */
76 return (void *) (long int) __hurd_dfail (fd
, err
);
78 switch (prot
& (PROT_READ
|PROT_WRITE
))
80 /* Although it apparently doesn't make sense to map a file with
81 protection set to PROT_NONE, it is actually sometimes done.
82 In particular, that's how localedef reserves some space for
83 the locale archive file, the rationale being that some
84 implementations take into account whether the mapping is
85 anonymous or not when selecting addresses. */
88 max_vmprot
= VM_PROT_READ
|VM_PROT_EXECUTE
;
90 max_vmprot
|= VM_PROT_WRITE
;
92 if (wobj
!= MACH_PORT_NULL
)
93 __mach_port_deallocate (__mach_task_self (), wobj
);
96 max_vmprot
= VM_PROT_WRITE
;
98 max_vmprot
|= VM_PROT_READ
|VM_PROT_EXECUTE
;
100 if (robj
!= MACH_PORT_NULL
)
101 __mach_port_deallocate (__mach_task_self (), robj
);
103 case PROT_READ
|PROT_WRITE
:
104 max_vmprot
= VM_PROT_ALL
;
108 /* Remove extra reference. */
109 __mach_port_deallocate (__mach_task_self (), memobj
);
111 else if (wobj
== MACH_PORT_NULL
/* Not writable by mapping. */
113 /* The file can only be mapped for reading. Since we are
114 making a private mapping, we will never try to write the
115 object anyway, so we don't care. */
119 __mach_port_deallocate (__mach_task_self (), wobj
);
120 return (void *) (long int) __hurd_fail (EACCES
);
124 __builtin_unreachable ();
127 /* XXX handle MAP_NOEXTEND */
131 /* XXX handle MAP_INHERIT */
134 max_vmprot
= VM_PROT_ALL
;
136 err
= __vm_map (__mach_task_self (),
137 &mapaddr
, (vm_size_t
) len
, (vm_address_t
) 0,
139 memobj
, (vm_offset_t
) offset
,
140 copy
, vmprot
, max_vmprot
,
141 copy
? VM_INHERIT_COPY
: VM_INHERIT_SHARE
);
143 if (flags
& MAP_FIXED
)
145 if (err
== KERN_NO_SPACE
)
147 /* XXX this is not atomic as it is in unix! */
148 /* The region is already allocated; deallocate it first. */
149 err
= __vm_deallocate (__mach_task_self (), mapaddr
, len
);
151 err
= __vm_map (__mach_task_self (),
152 &mapaddr
, (vm_size_t
) len
, (vm_address_t
) 0,
153 0, memobj
, (vm_offset_t
) offset
,
154 copy
, vmprot
, max_vmprot
,
155 copy
? VM_INHERIT_COPY
: VM_INHERIT_SHARE
);
160 if (mapaddr
!= 0 && (err
== KERN_NO_SPACE
|| err
== KERN_INVALID_ADDRESS
))
161 err
= __vm_map (__mach_task_self (),
162 &mapaddr
, (vm_size_t
) len
, (vm_address_t
) 0,
163 1, memobj
, (vm_offset_t
) offset
,
164 copy
, vmprot
, max_vmprot
,
165 copy
? VM_INHERIT_COPY
: VM_INHERIT_SHARE
);
168 if (memobj
!= MACH_PORT_NULL
)
169 __mach_port_deallocate (__mach_task_self (), memobj
);
171 if (err
== KERN_PROTECTION_FAILURE
)
175 return (void *) (long int) __hurd_fail (err
);
177 return (void *) mapaddr
;
180 libc_hidden_def (__mmap
)
181 weak_alias (__mmap
, mmap
)