]>
Commit | Line | Data |
---|---|---|
6408bdde | 1 | /* Copyright (C) 1994, 1995 Free Software Foundation, Inc. |
28f540f4 RM |
2 | This file is part of the GNU C Library. |
3 | ||
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. | |
8 | ||
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. | |
13 | ||
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 | |
16 | not, write to the Free Software Foundation, Inc., 675 Mass Ave, | |
17 | Cambridge, MA 02139, USA. */ | |
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 (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. */ | |
33 | ||
34 | caddr_t | |
6408bdde | 35 | __mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset) |
28f540f4 RM |
36 | { |
37 | error_t err; | |
38 | vm_prot_t vmprot; | |
39 | memory_object_t memobj; | |
40 | vm_address_t mapaddr; | |
41 | ||
42 | vmprot = VM_PROT_NONE; | |
43 | if (prot & PROT_READ) | |
44 | vmprot |= VM_PROT_READ; | |
45 | if (prot & PROT_WRITE) | |
46 | vmprot |= VM_PROT_WRITE; | |
47 | if (prot & PROT_EXEC) | |
48 | vmprot |= VM_PROT_EXECUTE; | |
49 | ||
50 | switch (flags & MAP_TYPE) | |
51 | { | |
52 | default: | |
53 | return (caddr_t) (long int) __hurd_fail (EINVAL); | |
54 | ||
55 | case MAP_ANON: | |
56 | memobj = MACH_PORT_NULL; | |
57 | break; | |
58 | ||
59 | case MAP_FILE: | |
7752137a | 60 | case 0: /* Allow, e.g., just MAP_SHARED. */ |
28f540f4 RM |
61 | { |
62 | mach_port_t robj, wobj; | |
63 | if (err = HURD_DPORT_USE (fd, __io_map (port, &robj, &wobj))) | |
64 | return (caddr_t) (long int) __hurd_dfail (fd, err); | |
65 | switch (prot & (PROT_READ|PROT_WRITE)) | |
66 | { | |
67 | case PROT_READ: | |
68 | memobj = robj; | |
69 | __mach_port_deallocate (__mach_task_self (), wobj); | |
70 | break; | |
71 | case PROT_WRITE: | |
72 | memobj = wobj; | |
73 | __mach_port_deallocate (__mach_task_self (), robj); | |
74 | break; | |
75 | case PROT_READ|PROT_WRITE: | |
76 | __mach_port_deallocate (__mach_task_self (), robj); | |
77 | if (robj == wobj) | |
78 | memobj = wobj; | |
79 | else | |
80 | { | |
81 | __mach_port_deallocate (__mach_task_self (), wobj); | |
82 | return ((caddr_t) (long int) | |
83 | __hurd_fail (EGRATUITOUS)); /* XXX */ | |
84 | } | |
85 | break; | |
86 | } | |
87 | break; | |
88 | /* XXX handle MAP_NOEXTEND */ | |
89 | } | |
90 | } | |
91 | ||
92 | mapaddr = (vm_address_t) addr; | |
93 | err = __vm_map (__mach_task_self (), | |
94 | &mapaddr, (vm_size_t) len, (vm_address_t) 0, | |
7752137a | 95 | ! (flags & MAP_FIXED), |
28f540f4 RM |
96 | memobj, (vm_offset_t) offset, |
97 | flags & (MAP_COPY|MAP_PRIVATE), | |
98 | vmprot, VM_PROT_ALL, | |
99 | flags & MAP_INHERIT); | |
100 | ||
101 | if (memobj != MACH_PORT_NULL) | |
102 | __mach_port_deallocate (__mach_task_self (), memobj); | |
103 | ||
104 | return err ? (caddr_t) (long int) __hurd_fail (err) : (caddr_t) mapaddr; | |
105 | } | |
6408bdde RM |
106 | |
107 | weak_alias (__mmap, mmap) | |
28f540f4 | 108 |