Distinguish ELOOP diagnosis threshold from SYMLOOP_MAX.
[platform/upstream/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, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <sys/types.h>
20 #include <sys/mman.h>
21 #include <errno.h>
22 #include <hurd.h>
23 #include <hurd/fd.h>
24
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.  */
33
34 __ptr_t
35 __mmap (__ptr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
36 {
37   error_t err;
38   vm_prot_t vmprot;
39   memory_object_t memobj;
40   vm_address_t mapaddr;
41
42   mapaddr = (vm_address_t) addr;
43
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);
47
48   if ((flags & (MAP_TYPE|MAP_INHERIT)) == MAP_ANON
49       && prot == (PROT_READ|PROT_WRITE)) /* cf VM_PROT_DEFAULT */
50     {
51       /* vm_allocate has (a little) less overhead in the kernel too.  */
52       err = __vm_allocate (__mach_task_self (), &mapaddr, len,
53                            mapaddr == NULL);
54
55       if (err == KERN_NO_SPACE)
56         {
57           if (flags & MAP_FIXED)
58             {
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);
62               if (!err)
63                 err = __vm_allocate (__mach_task_self (), &mapaddr, len, 0);
64             }
65           else if (mapaddr != NULL)
66             err = __vm_allocate (__mach_task_self (), &mapaddr, len, 1);
67         }
68
69       return err ? (__ptr_t) (long int) __hurd_fail (err) : (__ptr_t) mapaddr;
70     }
71
72   vmprot = VM_PROT_NONE;
73   if (prot & PROT_READ)
74     vmprot |= VM_PROT_READ;
75   if (prot & PROT_WRITE)
76     vmprot |= VM_PROT_WRITE;
77   if (prot & PROT_EXEC)
78     vmprot |= VM_PROT_EXECUTE;
79
80   switch (flags & MAP_TYPE)
81     {
82     default:
83       return (__ptr_t) (long int) __hurd_fail (EINVAL);
84
85     case MAP_ANON:
86       memobj = MACH_PORT_NULL;
87       break;
88
89     case MAP_FILE:
90     case 0:                     /* Allow, e.g., just MAP_SHARED.  */
91       {
92         mach_port_t robj, wobj;
93         if (err = HURD_DPORT_USE (fd, __io_map (port, &robj, &wobj)))
94           {
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);
98           }
99         switch (prot & (PROT_READ|PROT_WRITE))
100           {
101           case PROT_READ:
102             memobj = robj;
103             if (wobj != MACH_PORT_NULL)
104               __mach_port_deallocate (__mach_task_self (), wobj);
105             break;
106           case PROT_WRITE:
107             memobj = wobj;
108             if (robj != MACH_PORT_NULL)
109               __mach_port_deallocate (__mach_task_self (), robj);
110             break;
111           case PROT_READ|PROT_WRITE:
112             if (robj == wobj)
113               {
114                 memobj = wobj;
115                 /* Remove extra reference.  */
116                 __mach_port_deallocate (__mach_task_self (), memobj);
117               }
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.  */
123               memobj = robj;
124             else
125               {
126                 __mach_port_deallocate (__mach_task_self (), wobj);
127                 return (__ptr_t) (long int) __hurd_fail (EACCES);
128               }
129             break;
130           default:              /* impossible */
131             return 0;
132           }
133         break;
134         /* XXX handle MAP_NOEXTEND */
135       }
136     }
137
138   /* XXX handle MAP_INHERIT */
139
140   err = __vm_map (__mach_task_self (),
141                   &mapaddr, (vm_size_t) len, (vm_address_t) 0,
142                   mapaddr == NULL,
143                   memobj, (vm_offset_t) offset,
144                   ! (flags & MAP_SHARED),
145                   vmprot, VM_PROT_ALL,
146                   (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
147
148   if (err == KERN_NO_SPACE)
149     {
150       if (flags & MAP_FIXED)
151         {
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);
155           if (! err)
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),
160                             vmprot, VM_PROT_ALL,
161                             (flags & MAP_SHARED) ? VM_INHERIT_SHARE
162                             : VM_INHERIT_COPY);
163         }
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),
169                         vmprot, VM_PROT_ALL,
170                         (flags & MAP_SHARED) ? VM_INHERIT_SHARE
171                         : VM_INHERIT_COPY);
172     }
173
174   if (memobj != MACH_PORT_NULL)
175     __mach_port_deallocate (__mach_task_self (), memobj);
176
177   if (err)
178     return (__ptr_t) (long int) __hurd_fail (err);
179
180   return (__ptr_t) mapaddr;
181 }
182
183 weak_alias (__mmap, mmap)