Update.
[glibc.git] / sysdeps / mach / hurd / mmap.c
blobdc4b024bb8cdd8727935e3139426bfcfdc9edf17
1 /* Copyright (C) 1994, 1995, 1996, 1997 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <sys/types.h>
20 #include <sys/mman.h>
21 #include <errno.h>
22 #include <hurd.h>
23 #include <hurd/fd.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 (caddr_t) -1
31 for errors (in which case `errno' is set). A successful `mmap' call
32 deallocates any previous mapping for the affected region. */
34 caddr_t
35 __mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
37 error_t err;
38 vm_prot_t vmprot;
39 memory_object_t memobj;
40 vm_address_t mapaddr;
41 vm_size_t pageoff;
43 pageoff = offset & (vm_page_size - 1);
44 offset &= ~(vm_page_size - 1);
46 mapaddr = (vm_address_t) addr;
47 if (flags & MAP_FIXED)
49 /* A specific address is requested. It need not be page-aligned;
50 it just needs to be congruent with the object offset. */
51 if ((mapaddr & (vm_page_size - 1)) != pageoff)
52 return (caddr_t) (long int) __hurd_fail (EINVAL);
53 else
54 /* We will add back PAGEOFF after mapping. */
55 mapaddr -= pageoff;
58 vmprot = VM_PROT_NONE;
59 if (prot & PROT_READ)
60 vmprot |= VM_PROT_READ;
61 if (prot & PROT_WRITE)
62 vmprot |= VM_PROT_WRITE;
63 if (prot & PROT_EXEC)
64 vmprot |= VM_PROT_EXECUTE;
66 switch (flags & MAP_TYPE)
68 default:
69 return (caddr_t) (long int) __hurd_fail (EINVAL);
71 case MAP_ANON:
72 memobj = MACH_PORT_NULL;
73 break;
75 case MAP_FILE:
76 case 0: /* Allow, e.g., just MAP_SHARED. */
78 mach_port_t robj, wobj;
79 if (err = HURD_DPORT_USE (fd, __io_map (port, &robj, &wobj)))
80 return (caddr_t) (long int) __hurd_dfail (fd, err);
81 switch (prot & (PROT_READ|PROT_WRITE))
83 case PROT_READ:
84 memobj = robj;
85 if (wobj != MACH_PORT_NULL)
86 __mach_port_deallocate (__mach_task_self (), wobj);
87 break;
88 case PROT_WRITE:
89 memobj = wobj;
90 if (robj != MACH_PORT_NULL)
91 __mach_port_deallocate (__mach_task_self (), robj);
92 break;
93 case PROT_READ|PROT_WRITE:
94 if (robj == wobj)
96 memobj = wobj;
97 /* Remove extra reference. */
98 __mach_port_deallocate (__mach_task_self (), memobj);
100 else if (wobj == MACH_PORT_NULL && /* Not writable by mapping. */
101 !(flags & MAP_SHARED))
102 /* The file can only be mapped for reading. Since we are
103 making a private mapping, we will never try to write the
104 object anyway, so we don't care. */
105 memobj = robj;
106 else
108 __mach_port_deallocate (__mach_task_self (), wobj);
109 return (caddr_t) (long int) __hurd_fail (EACCES);
111 break;
113 break;
114 /* XXX handle MAP_NOEXTEND */
118 /* XXX handle MAP_INHERIT */
120 err = __vm_map (__mach_task_self (),
121 &mapaddr, (vm_size_t) len, (vm_address_t) 0,
122 ! (flags & MAP_FIXED),
123 memobj, (vm_offset_t) offset,
124 ! (flags & MAP_SHARED),
125 vmprot, VM_PROT_ALL,
126 (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
128 if (err == KERN_NO_SPACE && (flags & MAP_FIXED))
130 /* XXX this is not atomic as it is in unix! */
131 /* The region is already allocated; deallocate it first. */
132 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
133 if (! err)
134 err = __vm_map (__mach_task_self (),
135 &mapaddr, (vm_size_t) len, (vm_address_t) 0,
136 0, memobj, (vm_offset_t) offset,
137 ! (flags & MAP_SHARED),
138 vmprot, VM_PROT_ALL,
139 (flags & MAP_SHARED) ? VM_INHERIT_SHARE
140 : VM_INHERIT_COPY);
143 if (memobj != MACH_PORT_NULL)
144 __mach_port_deallocate (__mach_task_self (), memobj);
146 if (err)
147 return (caddr_t) (long int) __hurd_fail (err);
149 /* Adjust the mapping address for the offset-within-page. */
150 mapaddr += pageoff;
152 return (caddr_t) mapaddr;
155 weak_alias (__mmap, mmap)