1 | /* |
---|
2 | * Copyright (c) 2012 Chris Johns (chrisj@rtems.org) |
---|
3 | * Copyright (c) 2017 Gedare Bloom (gedare@rtems.org) |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #if HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <rtems.h> |
---|
15 | #include <errno.h> |
---|
16 | #include <stdlib.h> |
---|
17 | #include <sys/mman.h> |
---|
18 | |
---|
19 | #include <rtems/posix/mmanimpl.h> |
---|
20 | #include <rtems/posix/shmimpl.h> |
---|
21 | |
---|
22 | static void shm_munmap( rtems_libio_t *iop ) |
---|
23 | { |
---|
24 | POSIX_Shm_Control *shm = iop_to_shm( iop ); |
---|
25 | |
---|
26 | /* decrement mmap's shm reference_count and maybe delete the object */ |
---|
27 | POSIX_Shm_Attempt_delete(shm); |
---|
28 | } |
---|
29 | |
---|
30 | int munmap(void *addr, size_t len) |
---|
31 | { |
---|
32 | mmap_mapping *mapping; |
---|
33 | rtems_chain_node *node; |
---|
34 | uint32_t refcnt; |
---|
35 | |
---|
36 | /* |
---|
37 | * Clear errno. |
---|
38 | */ |
---|
39 | errno = 0; |
---|
40 | |
---|
41 | /* |
---|
42 | * Length cannot be 0. |
---|
43 | */ |
---|
44 | if ( len == 0 ) { |
---|
45 | errno = EINVAL; |
---|
46 | return -1; |
---|
47 | } |
---|
48 | |
---|
49 | /* |
---|
50 | * Obtain the mmap lock. Sets errno on failure. |
---|
51 | */ |
---|
52 | if ( !mmap_mappings_lock_obtain( )) |
---|
53 | return -1; |
---|
54 | |
---|
55 | node = rtems_chain_first (&mmap_mappings); |
---|
56 | while ( !rtems_chain_is_tail( &mmap_mappings, node )) { |
---|
57 | mapping = (mmap_mapping*) node; |
---|
58 | if ( ( addr >= mapping->addr ) && |
---|
59 | ( addr < ( mapping->addr + mapping->len )) ) { |
---|
60 | rtems_chain_extract( node ); |
---|
61 | /* FIXME: generally need a way to clean-up the backing object, but |
---|
62 | * currently it only matters for MAP_SHARED shm objects. */ |
---|
63 | if ( mapping->is_shared_shm == true ) { |
---|
64 | shm_munmap(mapping->iop); |
---|
65 | } |
---|
66 | refcnt = rtems_libio_decrement_mapping_refcnt(mapping->iop); |
---|
67 | if ( refcnt == 0 ) { |
---|
68 | rtems_libio_check_deferred_free(mapping->iop); |
---|
69 | } |
---|
70 | /* only free the mapping address for non-fixed mapping */ |
---|
71 | if (( mapping->flags & MAP_FIXED ) != MAP_FIXED ) { |
---|
72 | /* only free the mapping address for non-shared mapping, because we |
---|
73 | * re-use the mapping address across all of the shared mappings, and |
---|
74 | * it is memory managed independently... */ |
---|
75 | if (( mapping->flags & MAP_SHARED ) != MAP_SHARED ) { |
---|
76 | free( mapping->addr ); |
---|
77 | } |
---|
78 | } |
---|
79 | free( mapping ); |
---|
80 | break; |
---|
81 | } |
---|
82 | node = rtems_chain_next( node ); |
---|
83 | } |
---|
84 | |
---|
85 | mmap_mappings_lock_release( ); |
---|
86 | return 0; |
---|
87 | } |
---|