1 /* Copyright (C) 1994,95,96,97,99,2002 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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
;
43 mapaddr
= (vm_address_t
) addr
;
45 if ((flags
& (MAP_TYPE
|MAP_INHERIT
)) == MAP_ANON
46 && prot
== (PROT_READ
|PROT_WRITE
)) /* cf VM_PROT_DEFAULT */
48 /* vm_allocate has (a little) less overhead in the kernel too. */
49 err
= __vm_allocate (__mach_task_self (), &mapaddr
, len
,
50 !(flags
& MAP_FIXED
));
52 if (err
== KERN_NO_SPACE
&& (flags
& MAP_FIXED
))
54 /* XXX this is not atomic as it is in unix! */
55 /* The region is already allocated; deallocate it first. */
56 err
= __vm_deallocate (__mach_task_self (), mapaddr
, len
);
58 err
= __vm_allocate (__mach_task_self (), &mapaddr
, len
, 0);
61 return err
? (__ptr_t
) (long int) __hurd_fail (err
) : (__ptr_t
) mapaddr
;
64 pageoff
= offset
& (vm_page_size
- 1);
65 offset
&= ~(vm_page_size
- 1);
67 if (flags
& MAP_FIXED
)
69 /* A specific address is requested. It need not be page-aligned;
70 it just needs to be congruent with the object offset. */
71 if ((mapaddr
& (vm_page_size
- 1)) != pageoff
)
72 return (__ptr_t
) (long int) __hurd_fail (EINVAL
);
74 /* We will add back PAGEOFF after mapping. */
78 vmprot
= VM_PROT_NONE
;
80 vmprot
|= VM_PROT_READ
;
81 if (prot
& PROT_WRITE
)
82 vmprot
|= VM_PROT_WRITE
;
84 vmprot
|= VM_PROT_EXECUTE
;
86 switch (flags
& MAP_TYPE
)
89 return (__ptr_t
) (long int) __hurd_fail (EINVAL
);
92 memobj
= MACH_PORT_NULL
;
96 case 0: /* Allow, e.g., just MAP_SHARED. */
98 mach_port_t robj
, wobj
;
99 if (err
= HURD_DPORT_USE (fd
, __io_map (port
, &robj
, &wobj
)))
100 return (__ptr_t
) (long int) __hurd_dfail (fd
, err
);
101 switch (prot
& (PROT_READ
|PROT_WRITE
))
105 if (wobj
!= MACH_PORT_NULL
)
106 __mach_port_deallocate (__mach_task_self (), wobj
);
110 if (robj
!= MACH_PORT_NULL
)
111 __mach_port_deallocate (__mach_task_self (), robj
);
113 case PROT_READ
|PROT_WRITE
:
117 /* Remove extra reference. */
118 __mach_port_deallocate (__mach_task_self (), memobj
);
120 else if (wobj
== MACH_PORT_NULL
&& /* Not writable by mapping. */
121 !(flags
& MAP_SHARED
))
122 /* The file can only be mapped for reading. Since we are
123 making a private mapping, we will never try to write the
124 object anyway, so we don't care. */
128 __mach_port_deallocate (__mach_task_self (), wobj
);
129 return (__ptr_t
) (long int) __hurd_fail (EACCES
);
132 default: /* impossible */
136 /* XXX handle MAP_NOEXTEND */
140 /* XXX handle MAP_INHERIT */
142 err
= __vm_map (__mach_task_self (),
143 &mapaddr
, (vm_size_t
) len
, (vm_address_t
) 0,
144 ! (flags
& MAP_FIXED
),
145 memobj
, (vm_offset_t
) offset
,
146 ! (flags
& MAP_SHARED
),
148 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
: VM_INHERIT_COPY
);
150 if (err
== KERN_NO_SPACE
&& (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
165 if (memobj
!= MACH_PORT_NULL
)
166 __mach_port_deallocate (__mach_task_self (), memobj
);
169 return (__ptr_t
) (long int) __hurd_fail (err
);
171 /* Adjust the mapping address for the offset-within-page. */
174 return (__ptr_t
) mapaddr
;
177 weak_alias (__mmap
, mmap
)