1 /* Copyright (C) 1994,1995,1996,1997,1999,2002,2003,2004,2012
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <sys/types.h>
25 /* Map addresses starting near ADDR and extending for LEN bytes. from
26 OFFSET into the file FD describes according to PROT and FLAGS. If ADDR
27 is nonzero, it is the desired mapping address. If the MAP_FIXED bit is
28 set in FLAGS, the mapping will be at ADDR exactly (which must be
29 page-aligned); otherwise the system chooses a convenient nearby address.
30 The return value is the actual mapping address chosen or (__ptr_t) -1
31 for errors (in which case `errno' is set). A successful `mmap' call
32 deallocates any previous mapping for the affected region. */
35 __mmap (__ptr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
39 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 (__ptr_t) (long int) __hurd_fail (EINVAL);
48 if ((flags & (MAP_TYPE|MAP_INHERIT)) == MAP_ANON
49 && prot == (PROT_READ|PROT_WRITE)) /* cf VM_PROT_DEFAULT */
51 /* vm_allocate has (a little) less overhead in the kernel too. */
52 err = __vm_allocate (__mach_task_self (), &mapaddr, len,
55 if (err == KERN_NO_SPACE)
57 if (flags & MAP_FIXED)
59 /* XXX this is not atomic as it is in unix! */
60 /* The region is already allocated; deallocate it first. */
61 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
63 err = __vm_allocate (__mach_task_self (), &mapaddr, len, 0);
65 else if (mapaddr != NULL)
66 err = __vm_allocate (__mach_task_self (), &mapaddr, len, 1);
69 return err ? (__ptr_t) (long int) __hurd_fail (err) : (__ptr_t) mapaddr;
72 vmprot = VM_PROT_NONE;
74 vmprot |= VM_PROT_READ;
75 if (prot & PROT_WRITE)
76 vmprot |= VM_PROT_WRITE;
78 vmprot |= VM_PROT_EXECUTE;
80 switch (flags & MAP_TYPE)
83 return (__ptr_t) (long int) __hurd_fail (EINVAL);
86 memobj = MACH_PORT_NULL;
90 case 0: /* Allow, e.g., just MAP_SHARED. */
92 mach_port_t robj, wobj;
93 if (err = HURD_DPORT_USE (fd, __io_map (port, &robj, &wobj)))
95 if (err == MIG_BAD_ID || err == EOPNOTSUPP || err == ENOSYS)
96 err = ENODEV; /* File descriptor doesn't support mmap. */
97 return (__ptr_t) (long int) __hurd_dfail (fd, err);
99 switch (prot & (PROT_READ|PROT_WRITE))
103 if (wobj != MACH_PORT_NULL)
104 __mach_port_deallocate (__mach_task_self (), wobj);
108 if (robj != MACH_PORT_NULL)
109 __mach_port_deallocate (__mach_task_self (), robj);
111 case PROT_READ|PROT_WRITE:
115 /* Remove extra reference. */
116 __mach_port_deallocate (__mach_task_self (), memobj);
118 else if (wobj == MACH_PORT_NULL && /* Not writable by mapping. */
119 !(flags & MAP_SHARED))
120 /* The file can only be mapped for reading. Since we are
121 making a private mapping, we will never try to write the
122 object anyway, so we don't care. */
126 __mach_port_deallocate (__mach_task_self (), wobj);
127 return (__ptr_t) (long int) __hurd_fail (EACCES);
130 default: /* impossible */
134 /* XXX handle MAP_NOEXTEND */
138 /* XXX handle MAP_INHERIT */
140 err = __vm_map (__mach_task_self (),
141 &mapaddr, (vm_size_t) len, (vm_address_t) 0,
143 memobj, (vm_offset_t) offset,
144 ! (flags & MAP_SHARED),
146 (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
148 if (err == KERN_NO_SPACE)
150 if (flags & MAP_FIXED)
152 /* XXX this is not atomic as it is in unix! */
153 /* The region is already allocated; deallocate it first. */
154 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
156 err = __vm_map (__mach_task_self (),
157 &mapaddr, (vm_size_t) len, (vm_address_t) 0,
158 0, memobj, (vm_offset_t) offset,
159 ! (flags & MAP_SHARED),
161 (flags & MAP_SHARED) ? VM_INHERIT_SHARE
164 else if (mapaddr != NULL)
165 err = __vm_map (__mach_task_self (),
166 &mapaddr, (vm_size_t) len, (vm_address_t) 0,
167 1, memobj, (vm_offset_t) offset,
168 ! (flags & MAP_SHARED),
170 (flags & MAP_SHARED) ? VM_INHERIT_SHARE
174 if (memobj != MACH_PORT_NULL)
175 __mach_port_deallocate (__mach_task_self (), memobj);
178 return (__ptr_t) (long int) __hurd_fail (err);
180 return (__ptr_t) mapaddr;
183 weak_alias (__mmap, mmap)