1 /* Copyright (C) 1994-2013 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
,
54 if (err
== KERN_NO_SPACE
)
56 if (flags
& MAP_FIXED
)
58 /* XXX this is not atomic as it is in unix! */
59 /* The region is already allocated; deallocate it first. */
60 err
= __vm_deallocate (__mach_task_self (), mapaddr
, len
);
62 err
= __vm_allocate (__mach_task_self (), &mapaddr
, len
, 0);
64 else if (mapaddr
!= NULL
)
65 err
= __vm_allocate (__mach_task_self (), &mapaddr
, len
, 1);
68 return err
? (__ptr_t
) (long int) __hurd_fail (err
) : (__ptr_t
) mapaddr
;
71 vmprot
= VM_PROT_NONE
;
73 vmprot
|= VM_PROT_READ
;
74 if (prot
& PROT_WRITE
)
75 vmprot
|= VM_PROT_WRITE
;
77 vmprot
|= VM_PROT_EXECUTE
;
79 switch (flags
& MAP_TYPE
)
82 return (__ptr_t
) (long int) __hurd_fail (EINVAL
);
85 memobj
= MACH_PORT_NULL
;
89 case 0: /* Allow, e.g., just MAP_SHARED. */
91 mach_port_t robj
, wobj
;
92 if (err
= HURD_DPORT_USE (fd
, __io_map (port
, &robj
, &wobj
)))
94 if (err
== MIG_BAD_ID
|| err
== EOPNOTSUPP
|| err
== ENOSYS
)
95 err
= ENODEV
; /* File descriptor doesn't support mmap. */
96 return (__ptr_t
) (long int) __hurd_dfail (fd
, err
);
98 switch (prot
& (PROT_READ
|PROT_WRITE
))
102 if (wobj
!= MACH_PORT_NULL
)
103 __mach_port_deallocate (__mach_task_self (), wobj
);
107 if (robj
!= MACH_PORT_NULL
)
108 __mach_port_deallocate (__mach_task_self (), robj
);
110 case PROT_READ
|PROT_WRITE
:
114 /* Remove extra reference. */
115 __mach_port_deallocate (__mach_task_self (), memobj
);
117 else if (wobj
== MACH_PORT_NULL
&& /* Not writable by mapping. */
118 !(flags
& MAP_SHARED
))
119 /* The file can only be mapped for reading. Since we are
120 making a private mapping, we will never try to write the
121 object anyway, so we don't care. */
125 __mach_port_deallocate (__mach_task_self (), wobj
);
126 return (__ptr_t
) (long int) __hurd_fail (EACCES
);
129 default: /* impossible */
133 /* XXX handle MAP_NOEXTEND */
137 /* XXX handle MAP_INHERIT */
139 err
= __vm_map (__mach_task_self (),
140 &mapaddr
, (vm_size_t
) len
, (vm_address_t
) 0,
142 memobj
, (vm_offset_t
) offset
,
143 ! (flags
& MAP_SHARED
),
145 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
: VM_INHERIT_COPY
);
147 if (err
== KERN_NO_SPACE
)
149 if (flags
& MAP_FIXED
)
151 /* XXX this is not atomic as it is in unix! */
152 /* The region is already allocated; deallocate it first. */
153 err
= __vm_deallocate (__mach_task_self (), mapaddr
, len
);
155 err
= __vm_map (__mach_task_self (),
156 &mapaddr
, (vm_size_t
) len
, (vm_address_t
) 0,
157 0, memobj
, (vm_offset_t
) offset
,
158 ! (flags
& MAP_SHARED
),
160 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
163 else if (mapaddr
!= NULL
)
164 err
= __vm_map (__mach_task_self (),
165 &mapaddr
, (vm_size_t
) len
, (vm_address_t
) 0,
166 1, memobj
, (vm_offset_t
) offset
,
167 ! (flags
& MAP_SHARED
),
169 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
173 if (memobj
!= MACH_PORT_NULL
)
174 __mach_port_deallocate (__mach_task_self (), memobj
);
177 return (__ptr_t
) (long int) __hurd_fail (err
);
179 return (__ptr_t
) mapaddr
;
182 weak_alias (__mmap
, mmap
)