1 /* Copyright (C) 1994-2016 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 <http://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 (__ptr_t) -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 (__ptr_t addr
, size_t len
, int prot
, int flags
, int fd
, off_t offset
)
38 memory_object_t memobj
;
41 mapaddr
= (vm_address_t
) addr
;
43 /* ADDR and OFFSET must be page-aligned. */
44 if ((mapaddr
& (__vm_page_size
- 1)) || (offset
& (__vm_page_size
- 1)))
45 return (__ptr_t
) (long int) __hurd_fail (EINVAL
);
47 if ((flags
& (MAP_TYPE
|MAP_INHERIT
)) == MAP_ANON
48 && prot
== (PROT_READ
|PROT_WRITE
)) /* cf VM_PROT_DEFAULT */
50 /* vm_allocate has (a little) less overhead in the kernel too. */
51 err
= __vm_allocate (__mach_task_self (), &mapaddr
, len
, mapaddr
== 0);
53 if (err
== KERN_NO_SPACE
)
55 if (flags
& MAP_FIXED
)
57 /* XXX this is not atomic as it is in unix! */
58 /* The region is already allocated; deallocate it first. */
59 err
= __vm_deallocate (__mach_task_self (), mapaddr
, len
);
61 err
= __vm_allocate (__mach_task_self (), &mapaddr
, len
, 0);
63 else if (mapaddr
!= 0)
64 err
= __vm_allocate (__mach_task_self (), &mapaddr
, len
, 1);
67 return err
? (__ptr_t
) (long int) __hurd_fail (err
) : (__ptr_t
) mapaddr
;
70 vmprot
= VM_PROT_NONE
;
72 vmprot
|= VM_PROT_READ
;
73 if (prot
& PROT_WRITE
)
74 vmprot
|= VM_PROT_WRITE
;
76 vmprot
|= VM_PROT_EXECUTE
;
78 switch (flags
& MAP_TYPE
)
81 return (__ptr_t
) (long int) __hurd_fail (EINVAL
);
84 memobj
= MACH_PORT_NULL
;
88 case 0: /* Allow, e.g., just MAP_SHARED. */
90 mach_port_t robj
, wobj
;
91 if (err
= HURD_DPORT_USE (fd
, __io_map (port
, &robj
, &wobj
)))
93 if (err
== MIG_BAD_ID
|| err
== EOPNOTSUPP
|| err
== ENOSYS
)
94 err
= ENODEV
; /* File descriptor doesn't support mmap. */
95 return (__ptr_t
) (long int) __hurd_dfail (fd
, err
);
97 switch (prot
& (PROT_READ
|PROT_WRITE
))
99 /* Although it apparently doesn't make sense to map a file with
100 protection set to PROT_NONE, it is actually sometimes done.
101 In particular, that's how localedef reserves some space for
102 the locale archive file, the rationale being that some
103 implementations take into account whether the mapping is
104 anonymous or not when selecting addresses. */
108 if (wobj
!= MACH_PORT_NULL
)
109 __mach_port_deallocate (__mach_task_self (), wobj
);
113 if (robj
!= MACH_PORT_NULL
)
114 __mach_port_deallocate (__mach_task_self (), robj
);
116 case PROT_READ
|PROT_WRITE
:
120 /* Remove extra reference. */
121 __mach_port_deallocate (__mach_task_self (), memobj
);
123 else if (wobj
== MACH_PORT_NULL
&& /* Not writable by mapping. */
124 !(flags
& MAP_SHARED
))
125 /* The file can only be mapped for reading. Since we are
126 making a private mapping, we will never try to write the
127 object anyway, so we don't care. */
131 __mach_port_deallocate (__mach_task_self (), wobj
);
132 return (__ptr_t
) (long int) __hurd_fail (EACCES
);
136 __builtin_unreachable ();
139 /* XXX handle MAP_NOEXTEND */
143 /* XXX handle MAP_INHERIT */
145 err
= __vm_map (__mach_task_self (),
146 &mapaddr
, (vm_size_t
) len
, (vm_address_t
) 0,
148 memobj
, (vm_offset_t
) offset
,
149 ! (flags
& MAP_SHARED
),
151 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
: VM_INHERIT_COPY
);
153 if (err
== KERN_NO_SPACE
)
155 if (flags
& MAP_FIXED
)
157 /* XXX this is not atomic as it is in unix! */
158 /* The region is already allocated; deallocate it first. */
159 err
= __vm_deallocate (__mach_task_self (), mapaddr
, len
);
161 err
= __vm_map (__mach_task_self (),
162 &mapaddr
, (vm_size_t
) len
, (vm_address_t
) 0,
163 0, memobj
, (vm_offset_t
) offset
,
164 ! (flags
& MAP_SHARED
),
166 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
169 else if (mapaddr
!= 0)
170 err
= __vm_map (__mach_task_self (),
171 &mapaddr
, (vm_size_t
) len
, (vm_address_t
) 0,
172 1, memobj
, (vm_offset_t
) offset
,
173 ! (flags
& MAP_SHARED
),
175 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
179 if (memobj
!= MACH_PORT_NULL
)
180 __mach_port_deallocate (__mach_task_self (), memobj
);
183 return (__ptr_t
) (long int) __hurd_fail (err
);
185 return (__ptr_t
) mapaddr
;
188 weak_alias (__mmap
, mmap
)