Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / mach / hurd / mmap.c
blob5407ed9b65b8e1caaa6b5377d550f7c662acb9d7
1 /* Copyright (C) 1994-2014 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>
19 #include <sys/mman.h>
20 #include <errno.h>
21 #include <hurd.h>
22 #include <hurd/fd.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. */
33 __ptr_t
34 __mmap (__ptr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
36 error_t err;
37 vm_prot_t vmprot;
38 memory_object_t memobj;
39 vm_address_t mapaddr;
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,
52 mapaddr == NULL);
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);
61 if (!err)
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;
72 if (prot & PROT_READ)
73 vmprot |= VM_PROT_READ;
74 if (prot & PROT_WRITE)
75 vmprot |= VM_PROT_WRITE;
76 if (prot & PROT_EXEC)
77 vmprot |= VM_PROT_EXECUTE;
79 switch (flags & MAP_TYPE)
81 default:
82 return (__ptr_t) (long int) __hurd_fail (EINVAL);
84 case MAP_ANON:
85 memobj = MACH_PORT_NULL;
86 break;
88 case MAP_FILE:
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))
100 case PROT_READ:
101 memobj = robj;
102 if (wobj != MACH_PORT_NULL)
103 __mach_port_deallocate (__mach_task_self (), wobj);
104 break;
105 case PROT_WRITE:
106 memobj = wobj;
107 if (robj != MACH_PORT_NULL)
108 __mach_port_deallocate (__mach_task_self (), robj);
109 break;
110 case PROT_READ|PROT_WRITE:
111 if (robj == wobj)
113 memobj = wobj;
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. */
122 memobj = robj;
123 else
125 __mach_port_deallocate (__mach_task_self (), wobj);
126 return (__ptr_t) (long int) __hurd_fail (EACCES);
128 break;
129 default: /* impossible */
130 return 0;
132 break;
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,
141 mapaddr == NULL,
142 memobj, (vm_offset_t) offset,
143 ! (flags & MAP_SHARED),
144 vmprot, VM_PROT_ALL,
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);
154 if (! err)
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),
159 vmprot, VM_PROT_ALL,
160 (flags & MAP_SHARED) ? VM_INHERIT_SHARE
161 : VM_INHERIT_COPY);
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),
168 vmprot, VM_PROT_ALL,
169 (flags & MAP_SHARED) ? VM_INHERIT_SHARE
170 : VM_INHERIT_COPY);
173 if (memobj != MACH_PORT_NULL)
174 __mach_port_deallocate (__mach_task_self (), memobj);
176 if (err)
177 return (__ptr_t) (long int) __hurd_fail (err);
179 return (__ptr_t) mapaddr;
182 weak_alias (__mmap, mmap)