]>
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: | |
60 | { | |
61 | mach_port_t robj, wobj; | |
62 | if (err = HURD_DPORT_USE (fd, __io_map (port, &robj, &wobj))) | |
63 | return (caddr_t) (long int) __hurd_dfail (fd, err); | |
64 | switch (prot & (PROT_READ|PROT_WRITE)) | |
65 | { | |
66 | case PROT_READ: | |
67 | memobj = robj; | |
68 | __mach_port_deallocate (__mach_task_self (), wobj); | |
69 | break; | |
70 | case PROT_WRITE: | |
71 | memobj = wobj; | |
72 | __mach_port_deallocate (__mach_task_self (), robj); | |
73 | break; | |
74 | case PROT_READ|PROT_WRITE: | |
75 | __mach_port_deallocate (__mach_task_self (), robj); | |
76 | if (robj == wobj) | |
77 | memobj = wobj; | |
78 | else | |
79 | { | |
80 | __mach_port_deallocate (__mach_task_self (), wobj); | |
81 | return ((caddr_t) (long int) | |
82 | __hurd_fail (EGRATUITOUS)); /* XXX */ | |
83 | } | |
84 | break; | |
85 | } | |
86 | break; | |
87 | /* XXX handle MAP_NOEXTEND */ | |
88 | } | |
89 | } | |
90 | ||
91 | mapaddr = (vm_address_t) addr; | |
92 | err = __vm_map (__mach_task_self (), | |
93 | &mapaddr, (vm_size_t) len, (vm_address_t) 0, | |
94 | flags & MAP_FIXED, | |
95 | memobj, (vm_offset_t) offset, | |
96 | flags & (MAP_COPY|MAP_PRIVATE), | |
97 | vmprot, VM_PROT_ALL, | |
98 | flags & MAP_INHERIT); | |
99 | ||
100 | if (memobj != MACH_PORT_NULL) | |
101 | __mach_port_deallocate (__mach_task_self (), memobj); | |
102 | ||
103 | return err ? (caddr_t) (long int) __hurd_fail (err) : (caddr_t) mapaddr; | |
104 | } | |
6408bdde RM |
105 | |
106 | weak_alias (__mmap, mmap) | |
28f540f4 | 107 |