Rev 2089 | Rev 2134 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2089 | Rev 2132 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Jakub Jermar |
2 | * Copyright (c) 2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericmm |
29 | /** @addtogroup genericmm |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Backend for anonymous memory address space areas. |
35 | * @brief Backend for anonymous memory address space areas. |
36 | * |
36 | * |
37 | */ |
37 | */ |
38 | 38 | ||
39 | #include <mm/as.h> |
39 | #include <mm/as.h> |
40 | #include <mm/page.h> |
40 | #include <mm/page.h> |
41 | #include <genarch/mm/page_pt.h> |
41 | #include <genarch/mm/page_pt.h> |
42 | #include <genarch/mm/page_ht.h> |
42 | #include <genarch/mm/page_ht.h> |
43 | #include <mm/frame.h> |
43 | #include <mm/frame.h> |
44 | #include <mm/slab.h> |
44 | #include <mm/slab.h> |
45 | #include <synch/mutex.h> |
45 | #include <synch/mutex.h> |
46 | #include <adt/list.h> |
46 | #include <adt/list.h> |
47 | #include <adt/btree.h> |
47 | #include <adt/btree.h> |
48 | #include <errno.h> |
48 | #include <errno.h> |
49 | #include <arch/types.h> |
49 | #include <arch/types.h> |
50 | #include <align.h> |
50 | #include <align.h> |
51 | #include <arch.h> |
51 | #include <arch.h> |
52 | 52 | ||
53 | #ifdef CONFIG_VIRT_IDX_DCACHE |
53 | #ifdef CONFIG_VIRT_IDX_DCACHE |
54 | #include <arch/mm/cache.h> |
54 | #include <arch/mm/cache.h> |
55 | #endif |
55 | #endif |
56 | 56 | ||
57 | static int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access); |
57 | static int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access); |
58 | static void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame); |
58 | static void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame); |
59 | static void anon_share(as_area_t *area); |
59 | static void anon_share(as_area_t *area); |
60 | 60 | ||
61 | mem_backend_t anon_backend = { |
61 | mem_backend_t anon_backend = { |
62 | .page_fault = anon_page_fault, |
62 | .page_fault = anon_page_fault, |
63 | .frame_free = anon_frame_free, |
63 | .frame_free = anon_frame_free, |
64 | .share = anon_share |
64 | .share = anon_share |
65 | }; |
65 | }; |
66 | 66 | ||
67 | /** Service a page fault in the anonymous memory address space area. |
67 | /** Service a page fault in the anonymous memory address space area. |
68 | * |
68 | * |
69 | * The address space area and page tables must be already locked. |
69 | * The address space area and page tables must be already locked. |
70 | * |
70 | * |
71 | * @param area Pointer to the address space area. |
71 | * @param area Pointer to the address space area. |
72 | * @param addr Faulting virtual address. |
72 | * @param addr Faulting virtual address. |
73 | * @param access Access mode that caused the fault (i.e. read/write/exec). |
73 | * @param access Access mode that caused the fault (i.e. read/write/exec). |
74 | * |
74 | * |
75 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e. serviced). |
75 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e. |
- | 76 | * serviced). |
|
76 | */ |
77 | */ |
77 | int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access) |
78 | int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access) |
78 | { |
79 | { |
79 | uintptr_t frame; |
80 | uintptr_t frame; |
80 | 81 | ||
81 | if (!as_area_check_access(area, access)) |
82 | if (!as_area_check_access(area, access)) |
82 | return AS_PF_FAULT; |
83 | return AS_PF_FAULT; |
83 | 84 | ||
84 | if (area->sh_info) { |
85 | if (area->sh_info) { |
85 | btree_node_t *leaf; |
86 | btree_node_t *leaf; |
86 | 87 | ||
87 | /* |
88 | /* |
88 | * The area is shared, chances are that the mapping can be found |
89 | * The area is shared, chances are that the mapping can be found |
89 | * in the pagemap of the address space area share info structure. |
90 | * in the pagemap of the address space area share info |
- | 91 | * structure. |
|
90 | * In the case that the pagemap does not contain the respective |
92 | * In the case that the pagemap does not contain the respective |
91 | * mapping, a new frame is allocated and the mapping is created. |
93 | * mapping, a new frame is allocated and the mapping is created. |
92 | */ |
94 | */ |
93 | mutex_lock(&area->sh_info->lock); |
95 | mutex_lock(&area->sh_info->lock); |
94 | frame = (uintptr_t) btree_search(&area->sh_info->pagemap, |
96 | frame = (uintptr_t) btree_search(&area->sh_info->pagemap, |
95 | ALIGN_DOWN(addr, PAGE_SIZE) - area->base, &leaf); |
97 | ALIGN_DOWN(addr, PAGE_SIZE) - area->base, &leaf); |
96 | if (!frame) { |
98 | if (!frame) { |
97 | bool allocate = true; |
99 | bool allocate = true; |
98 | int i; |
100 | int i; |
99 | 101 | ||
100 | /* |
102 | /* |
101 | * Zero can be returned as a valid frame address. |
103 | * Zero can be returned as a valid frame address. |
102 | * Just a small workaround. |
104 | * Just a small workaround. |
103 | */ |
105 | */ |
104 | for (i = 0; i < leaf->keys; i++) { |
106 | for (i = 0; i < leaf->keys; i++) { |
- | 107 | if (leaf->key[i] == |
|
105 | if (leaf->key[i] == ALIGN_DOWN(addr, PAGE_SIZE)) { |
108 | ALIGN_DOWN(addr, PAGE_SIZE)) { |
106 | allocate = false; |
109 | allocate = false; |
107 | break; |
110 | break; |
108 | } |
111 | } |
109 | } |
112 | } |
110 | if (allocate) { |
113 | if (allocate) { |
111 | frame = (uintptr_t) frame_alloc(ONE_FRAME, 0); |
114 | frame = (uintptr_t) frame_alloc(ONE_FRAME, 0); |
112 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
115 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
113 | 116 | ||
114 | /* |
117 | /* |
115 | * Insert the address of the newly allocated frame to the pagemap. |
118 | * Insert the address of the newly allocated |
- | 119 | * frame to the pagemap. |
|
116 | */ |
120 | */ |
- | 121 | btree_insert(&area->sh_info->pagemap, |
|
117 | btree_insert(&area->sh_info->pagemap, ALIGN_DOWN(addr, PAGE_SIZE) - area->base, (void *) frame, leaf); |
122 | ALIGN_DOWN(addr, PAGE_SIZE) - area->base, |
- | 123 | (void *) frame, leaf); |
|
118 | } |
124 | } |
119 | } |
125 | } |
120 | frame_reference_add(ADDR2PFN(frame)); |
126 | frame_reference_add(ADDR2PFN(frame)); |
121 | mutex_unlock(&area->sh_info->lock); |
127 | mutex_unlock(&area->sh_info->lock); |
122 | } else { |
128 | } else { |
123 | 129 | ||
124 | /* |
130 | /* |
125 | * In general, there can be several reasons that |
131 | * In general, there can be several reasons that |
126 | * can have caused this fault. |
132 | * can have caused this fault. |
127 | * |
133 | * |
128 | * - non-existent mapping: the area is an anonymous |
134 | * - non-existent mapping: the area is an anonymous |
129 | * area (e.g. heap or stack) and so far has not been |
135 | * area (e.g. heap or stack) and so far has not been |
130 | * allocated a frame for the faulting page |
136 | * allocated a frame for the faulting page |
131 | * |
137 | * |
132 | * - non-present mapping: another possibility, |
138 | * - non-present mapping: another possibility, |
133 | * currently not implemented, would be frame |
139 | * currently not implemented, would be frame |
134 | * reuse; when this becomes a possibility, |
140 | * reuse; when this becomes a possibility, |
135 | * do not forget to distinguish between |
141 | * do not forget to distinguish between |
136 | * the different causes |
142 | * the different causes |
137 | */ |
143 | */ |
138 | frame = (uintptr_t) frame_alloc(ONE_FRAME, 0); |
144 | frame = (uintptr_t) frame_alloc(ONE_FRAME, 0); |
139 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
145 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
140 | } |
146 | } |
141 | 147 | ||
142 | /* |
148 | /* |
143 | * Map 'page' to 'frame'. |
149 | * Map 'page' to 'frame'. |
144 | * Note that TLB shootdown is not attempted as only new information is being |
150 | * Note that TLB shootdown is not attempted as only new information is |
145 | * inserted into page tables. |
151 | * being inserted into page tables. |
146 | */ |
152 | */ |
147 | page_mapping_insert(AS, addr, frame, as_area_get_flags(area)); |
153 | page_mapping_insert(AS, addr, frame, as_area_get_flags(area)); |
148 | if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1)) |
154 | if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1)) |
149 | panic("Could not insert used space.\n"); |
155 | panic("Could not insert used space.\n"); |
150 | 156 | ||
151 | return AS_PF_OK; |
157 | return AS_PF_OK; |
152 | } |
158 | } |
153 | 159 | ||
154 | /** Free a frame that is backed by the anonymous memory backend. |
160 | /** Free a frame that is backed by the anonymous memory backend. |
155 | * |
161 | * |
156 | * The address space area and page tables must be already locked. |
162 | * The address space area and page tables must be already locked. |
157 | * |
163 | * |
158 | * @param area Ignored. |
164 | * @param area Ignored. |
159 | * @param page Virtual address of the page corresponding to the frame. |
165 | * @param page Virtual address of the page corresponding to the frame. |
160 | * @param frame Frame to be released. |
166 | * @param frame Frame to be released. |
161 | */ |
167 | */ |
162 | void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame) |
168 | void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame) |
163 | { |
169 | { |
164 | frame_free(frame); |
170 | frame_free(frame); |
165 | #ifdef CONFIG_VIRT_IDX_DCACHE |
171 | #ifdef CONFIG_VIRT_IDX_DCACHE |
166 | dcache_flush_frame(page, frame); |
172 | dcache_flush_frame(page, frame); |
167 | #endif |
173 | #endif |
168 | } |
174 | } |
169 | 175 | ||
170 | /** Share the anonymous address space area. |
176 | /** Share the anonymous address space area. |
171 | * |
177 | * |
172 | * Sharing of anonymous area is done by duplicating its entire mapping |
178 | * Sharing of anonymous area is done by duplicating its entire mapping |
173 | * to the pagemap. Page faults will primarily search for frames there. |
179 | * to the pagemap. Page faults will primarily search for frames there. |
174 | * |
180 | * |
175 | * The address space and address space area must be already locked. |
181 | * The address space and address space area must be already locked. |
176 | * |
182 | * |
177 | * @param area Address space area to be shared. |
183 | * @param area Address space area to be shared. |
178 | */ |
184 | */ |
179 | void anon_share(as_area_t *area) |
185 | void anon_share(as_area_t *area) |
180 | { |
186 | { |
181 | link_t *cur; |
187 | link_t *cur; |
182 | 188 | ||
183 | /* |
189 | /* |
184 | * Copy used portions of the area to sh_info's page map. |
190 | * Copy used portions of the area to sh_info's page map. |
185 | */ |
191 | */ |
186 | mutex_lock(&area->sh_info->lock); |
192 | mutex_lock(&area->sh_info->lock); |
- | 193 | for (cur = area->used_space.leaf_head.next; |
|
187 | for (cur = area->used_space.leaf_head.next; cur != &area->used_space.leaf_head; cur = cur->next) { |
194 | cur != &area->used_space.leaf_head; cur = cur->next) { |
188 | btree_node_t *node; |
195 | btree_node_t *node; |
189 | int i; |
196 | int i; |
190 | 197 | ||
191 | node = list_get_instance(cur, btree_node_t, leaf_link); |
198 | node = list_get_instance(cur, btree_node_t, leaf_link); |
192 | for (i = 0; i < node->keys; i++) { |
199 | for (i = 0; i < node->keys; i++) { |
193 | uintptr_t base = node->key[i]; |
200 | uintptr_t base = node->key[i]; |
194 | count_t count = (count_t) node->value[i]; |
201 | count_t count = (count_t) node->value[i]; |
195 | int j; |
202 | int j; |
196 | 203 | ||
197 | for (j = 0; j < count; j++) { |
204 | for (j = 0; j < count; j++) { |
198 | pte_t *pte; |
205 | pte_t *pte; |
199 | 206 | ||
200 | page_table_lock(area->as, false); |
207 | page_table_lock(area->as, false); |
201 | pte = page_mapping_find(area->as, base + j*PAGE_SIZE); |
208 | pte = page_mapping_find(area->as, |
- | 209 | base + j * PAGE_SIZE); |
|
202 | ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte)); |
210 | ASSERT(pte && PTE_VALID(pte) && |
- | 211 | PTE_PRESENT(pte)); |
|
203 | btree_insert(&area->sh_info->pagemap, (base + j*PAGE_SIZE) - area->base, |
212 | btree_insert(&area->sh_info->pagemap, |
- | 213 | (base + j * PAGE_SIZE) - area->base, |
|
204 | (void *) PTE_GET_FRAME(pte), NULL); |
214 | (void *) PTE_GET_FRAME(pte), NULL); |
205 | page_table_unlock(area->as, false); |
215 | page_table_unlock(area->as, false); |
- | 216 | ||
206 | frame_reference_add(ADDR2PFN(PTE_GET_FRAME(pte))); |
217 | pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(pte)); |
- | 218 | frame_reference_add(pfn); |
|
207 | } |
219 | } |
208 | 220 | ||
209 | } |
221 | } |
210 | } |
222 | } |
211 | mutex_unlock(&area->sh_info->lock); |
223 | mutex_unlock(&area->sh_info->lock); |
212 | } |
224 | } |
213 | 225 | ||
214 | /** @} |
226 | /** @} |
215 | */ |
227 | */ |
- | 228 | ||
216 | 229 |