-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmap.c
233 lines (204 loc) · 5.47 KB
/
mmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* mmap() - POSIX 1003.1b 6.3.1 - map pages of memory
*
* COPYRIGHT (c) 2012.
* Chris Johns ([email protected])
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <rtems.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "rtems/libio_.h"
#include "mmap-internal.h"
#define RTEMS_MUTEX_ATTRIBS \
(RTEMS_PRIORITY | RTEMS_SIMPLE_BINARY_SEMAPHORE | \
RTEMS_NO_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL)
/**
* Mmap chain of mappings.
*/
rtems_chain_control mmap_mappings;
/**
* The id of the MMAP lock.
*/
static rtems_id mmap_mappings_lock;
/**
* Create the lock.
*/
static
bool mmap_mappings_lock_create(
void
)
{
/*
* Lock the mapping table. We only create a lock if a call is made. First we
* test if a mapping lock is present. If one is present we lock it. If not
* the libio lock is locked and we then test the mapping lock again. If not
* present we create the mapping lock then release libio lock.
*/
if ( mmap_mappings_lock == 0 ) {
rtems_status_code sc = RTEMS_SUCCESSFUL;
rtems_chain_initialize_empty( &mmap_mappings );
rtems_semaphore_obtain( rtems_libio_semaphore,
RTEMS_WAIT, RTEMS_NO_TIMEOUT );
if ( mmap_mappings_lock == 0 )
sc = rtems_semaphore_create( rtems_build_name( 'M', 'M', 'A', 'P' ),
1,
RTEMS_MUTEX_ATTRIBS,
RTEMS_NO_PRIORITY,
&mmap_mappings_lock );
rtems_semaphore_release( rtems_libio_semaphore );
if ( sc != RTEMS_SUCCESSFUL ) {
errno = EINVAL;
return false;
}
}
return true;
}
bool mmap_mappings_lock_obtain(
void
)
{
if ( mmap_mappings_lock_create( ) ) {
rtems_status_code sc;
sc = rtems_semaphore_obtain( mmap_mappings_lock,
RTEMS_WAIT, RTEMS_NO_TIMEOUT );
if ( sc != RTEMS_SUCCESSFUL ) {
errno = EINVAL;
return false;
}
}
return true;
}
bool mmap_mappings_lock_release(
void
)
{
rtems_status_code sc;
sc = rtems_semaphore_release( mmap_mappings_lock );
if (( sc != RTEMS_SUCCESSFUL ) && ( errno == 0 )) {
errno = EINVAL;
return false;
}
return true;
}
void *mmap(
void *addr, size_t len, int prot, int flags, int fildes, off_t off
)
{
struct stat sb;
mmap_mapping* mapping;
ssize_t r;
/*
* Clear errno.
*/
errno = 0;
/*
* Get a stat of the file to get the dev + inode number and to make sure the
* fd is ok. The normal libio calls cannot be used because we need to return
* MAP_FAILED on error and they return -1 directly without coming back to
* here.
*/
if ( fstat( fildes, &sb ) < 0 ) {
errno = EBADF;
return MAP_FAILED;
}
if ( len == 0 ) {
errno = EINVAL;
return MAP_FAILED;
}
/*
* Check the type of file we have and make sure it is supported.
*/
if ( S_ISDIR( sb.st_mode ) || S_ISLNK( sb.st_mode )) {
errno = ENODEV;
return MAP_FAILED;
}
/*
* We can provide read, write and execute because the memory in RTEMS does
* not normally have protections but we cannot hide access to memory.
*/
if ( prot == PROT_NONE ) {
errno = EINVAL;
return MAP_FAILED;
}
/*
* Check to see if the mapping is valid for the file.
*/
if ( S_ISREG( sb.st_mode )
&& (( off >= sb.st_size ) || (( off + len ) >= sb.st_size ))) {
errno = EOVERFLOW;
return MAP_FAILED;
}
/*
* Obtain the mmap lock. Sets errno on failure.
*/
if ( !mmap_mappings_lock_obtain( ) )
return MAP_FAILED;
if (( flags & MAP_FIXED ) == MAP_FIXED ) {
rtems_chain_node* node = rtems_chain_first (&mmap_mappings);
while ( !rtems_chain_is_tail( &mmap_mappings, node )) {
/*
* If the map is fixed see if this address is already mapped. At this
* point in time if there is an overlap in the mappings we return an
* error.
*/
mapping = (mmap_mapping*) node;
if ( ( addr >= mapping->addr ) &&
( addr < ( mapping->addr + mapping->len )) ) {
mmap_mappings_lock_release( );
errno = ENXIO;
return MAP_FAILED;
}
node = rtems_chain_next( node );
}
}
mapping = malloc( sizeof( mmap_mapping ));
if ( !mapping ) {
mmap_mappings_lock_release( );
errno = ENOMEM;
return NULL;
}
memset( mapping, 0, sizeof( mmap_mapping ));
mapping->len = len;
mapping->flags = flags;
if (( flags & MAP_FIXED ) != MAP_FIXED ) {
mapping->addr = malloc( len );
if ( !mapping->addr ) {
mmap_mappings_lock_release( );
free( mapping );
errno = ENOMEM;
return MAP_FAILED;
}
/*
* Do not seek on character devices, pipes or sockets.
*/
if ( S_ISREG( sb.st_mode ) || S_ISBLK( sb.st_mode ) ) {
if ( lseek( fildes, off, SEEK_SET ) < 0 ) {
mmap_mappings_lock_release( );
free( mapping->addr );
free( mapping );
return MAP_FAILED;
}
}
}
r = read( fildes, mapping->addr, len );
if ( r != len ) {
mmap_mappings_lock_release( );
free( mapping->addr );
free( mapping );
errno = ENXIO;
return MAP_FAILED;
}
rtems_chain_append( &mmap_mappings, &mapping->node );
mmap_mappings_lock_release( );
return mapping->addr;
}