]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/mmap.c
e08e999fc9fac11829ee435d4a5e2d31aa2cca37
[thirdparty/glibc.git] / sysdeps / mach / hurd / mmap.c
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.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #include <sys/types.h>
21 #include <sys/mman.h>
22 #include <errno.h>
23 #include <hurd.h>
24 #include <hurd/fd.h>
25
26 /* Map addresses starting near ADDR and extending for LEN bytes. from
27 OFFSET into the file FD describes according to PROT and FLAGS. If ADDR
28 is nonzero, it is the desired mapping address. If the MAP_FIXED bit is
29 set in FLAGS, the mapping will be at ADDR exactly (which must be
30 page-aligned); otherwise the system chooses a convenient nearby address.
31 The return value is the actual mapping address chosen or (__ptr_t) -1
32 for errors (in which case `errno' is set). A successful `mmap' call
33 deallocates any previous mapping for the affected region. */
34
35 __ptr_t
36 __mmap (__ptr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
37 {
38 error_t err;
39 vm_prot_t vmprot;
40 memory_object_t memobj;
41 vm_address_t mapaddr;
42
43 mapaddr = (vm_address_t) addr;
44
45 /* ADDR and OFFSET must be page-aligned. */
46 if ((mapaddr & (vm_page_size - 1)) || (offset & (vm_page_size - 1)))
47 return (__ptr_t) (long int) __hurd_fail (EINVAL);
48
49 if ((flags & (MAP_TYPE|MAP_INHERIT)) == MAP_ANON
50 && prot == (PROT_READ|PROT_WRITE)) /* cf VM_PROT_DEFAULT */
51 {
52 /* vm_allocate has (a little) less overhead in the kernel too. */
53 err = __vm_allocate (__mach_task_self (), &mapaddr, len,
54 mapaddr == NULL);
55
56 if (err == KERN_NO_SPACE)
57 {
58 if (flags & MAP_FIXED)
59 {
60 /* XXX this is not atomic as it is in unix! */
61 /* The region is already allocated; deallocate it first. */
62 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
63 if (!err)
64 err = __vm_allocate (__mach_task_self (), &mapaddr, len, 0);
65 }
66 else if (mapaddr != NULL)
67 err = __vm_allocate (__mach_task_self (), &mapaddr, len, 1);
68 }
69
70 return err ? (__ptr_t) (long int) __hurd_fail (err) : (__ptr_t) mapaddr;
71 }
72
73 vmprot = VM_PROT_NONE;
74 if (prot & PROT_READ)
75 vmprot |= VM_PROT_READ;
76 if (prot & PROT_WRITE)
77 vmprot |= VM_PROT_WRITE;
78 if (prot & PROT_EXEC)
79 vmprot |= VM_PROT_EXECUTE;
80
81 switch (flags & MAP_TYPE)
82 {
83 default:
84 return (__ptr_t) (long int) __hurd_fail (EINVAL);
85
86 case MAP_ANON:
87 memobj = MACH_PORT_NULL;
88 break;
89
90 case MAP_FILE:
91 case 0: /* Allow, e.g., just MAP_SHARED. */
92 {
93 mach_port_t robj, wobj;
94 if (err = HURD_DPORT_USE (fd, __io_map (port, &robj, &wobj)))
95 {
96 if (err == MIG_BAD_ID || err == EOPNOTSUPP || err == ENOSYS)
97 err = ENODEV; /* File descriptor doesn't support mmap. */
98 return (__ptr_t) (long int) __hurd_dfail (fd, err);
99 }
100 switch (prot & (PROT_READ|PROT_WRITE))
101 {
102 case PROT_READ:
103 memobj = robj;
104 if (wobj != MACH_PORT_NULL)
105 __mach_port_deallocate (__mach_task_self (), wobj);
106 break;
107 case PROT_WRITE:
108 memobj = wobj;
109 if (robj != MACH_PORT_NULL)
110 __mach_port_deallocate (__mach_task_self (), robj);
111 break;
112 case PROT_READ|PROT_WRITE:
113 if (robj == wobj)
114 {
115 memobj = wobj;
116 /* Remove extra reference. */
117 __mach_port_deallocate (__mach_task_self (), memobj);
118 }
119 else if (wobj == MACH_PORT_NULL && /* Not writable by mapping. */
120 !(flags & MAP_SHARED))
121 /* The file can only be mapped for reading. Since we are
122 making a private mapping, we will never try to write the
123 object anyway, so we don't care. */
124 memobj = robj;
125 else
126 {
127 __mach_port_deallocate (__mach_task_self (), wobj);
128 return (__ptr_t) (long int) __hurd_fail (EACCES);
129 }
130 break;
131 default: /* impossible */
132 return 0;
133 }
134 break;
135 /* XXX handle MAP_NOEXTEND */
136 }
137 }
138
139 /* XXX handle MAP_INHERIT */
140
141 err = __vm_map (__mach_task_self (),
142 &mapaddr, (vm_size_t) len, (vm_address_t) 0,
143 mapaddr == NULL,
144 memobj, (vm_offset_t) offset,
145 ! (flags & MAP_SHARED),
146 vmprot, VM_PROT_ALL,
147 (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
148
149 if (err == KERN_NO_SPACE)
150 {
151 if (flags & MAP_FIXED)
152 {
153 /* XXX this is not atomic as it is in unix! */
154 /* The region is already allocated; deallocate it first. */
155 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
156 if (! err)
157 err = __vm_map (__mach_task_self (),
158 &mapaddr, (vm_size_t) len, (vm_address_t) 0,
159 0, memobj, (vm_offset_t) offset,
160 ! (flags & MAP_SHARED),
161 vmprot, VM_PROT_ALL,
162 (flags & MAP_SHARED) ? VM_INHERIT_SHARE
163 : VM_INHERIT_COPY);
164 }
165 else if (mapaddr != NULL)
166 err = __vm_map (__mach_task_self (),
167 &mapaddr, (vm_size_t) len, (vm_address_t) 0,
168 1, memobj, (vm_offset_t) offset,
169 ! (flags & MAP_SHARED),
170 vmprot, VM_PROT_ALL,
171 (flags & MAP_SHARED) ? VM_INHERIT_SHARE
172 : VM_INHERIT_COPY);
173 }
174
175 if (memobj != MACH_PORT_NULL)
176 __mach_port_deallocate (__mach_task_self (), memobj);
177
178 if (err)
179 return (__ptr_t) (long int) __hurd_fail (err);
180
181 return (__ptr_t) mapaddr;
182 }
183
184 weak_alias (__mmap, mmap)