Rev 1426 | Rev 1702 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1426 | Rev 1545 | ||
---|---|---|---|
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 | /** |
29 | /** |
30 | * @file backend_anon.c |
30 | * @file backend_anon.c |
31 | * @brief Backend for anonymous memory address space areas. |
31 | * @brief Backend for anonymous memory address space areas. |
32 | * |
32 | * |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #include <mm/as.h> |
35 | #include <mm/as.h> |
36 | #include <mm/page.h> |
36 | #include <mm/page.h> |
37 | #include <genarch/mm/page_pt.h> |
37 | #include <genarch/mm/page_pt.h> |
38 | #include <genarch/mm/page_ht.h> |
38 | #include <genarch/mm/page_ht.h> |
39 | #include <mm/frame.h> |
39 | #include <mm/frame.h> |
40 | #include <mm/slab.h> |
40 | #include <mm/slab.h> |
41 | #include <synch/mutex.h> |
41 | #include <synch/mutex.h> |
42 | #include <adt/list.h> |
42 | #include <adt/list.h> |
43 | #include <adt/btree.h> |
43 | #include <adt/btree.h> |
44 | #include <errno.h> |
44 | #include <errno.h> |
45 | #include <arch/types.h> |
45 | #include <arch/types.h> |
46 | #include <typedefs.h> |
46 | #include <typedefs.h> |
47 | #include <align.h> |
47 | #include <align.h> |
48 | #include <arch.h> |
48 | #include <arch.h> |
49 | 49 | ||
50 | static int anon_page_fault(as_area_t *area, __address addr, pf_access_t access); |
50 | static int anon_page_fault(as_area_t *area, __address addr, pf_access_t access); |
51 | static void anon_frame_free(as_area_t *area, __address page, __address frame); |
51 | static void anon_frame_free(as_area_t *area, __address page, __address frame); |
52 | static void anon_share(as_area_t *area); |
52 | static void anon_share(as_area_t *area); |
53 | 53 | ||
54 | mem_backend_t anon_backend = { |
54 | mem_backend_t anon_backend = { |
55 | .page_fault = anon_page_fault, |
55 | .page_fault = anon_page_fault, |
56 | .frame_free = anon_frame_free, |
56 | .frame_free = anon_frame_free, |
57 | .share = anon_share |
57 | .share = anon_share |
58 | }; |
58 | }; |
59 | 59 | ||
60 | /** Service a page fault in the anonymous memory address space area. |
60 | /** Service a page fault in the anonymous memory address space area. |
61 | * |
61 | * |
62 | * The address space area and page tables must be already locked. |
62 | * The address space area and page tables must be already locked. |
63 | * |
63 | * |
64 | * @param area Pointer to the address space area. |
64 | * @param area Pointer to the address space area. |
65 | * @param addr Faulting virtual address. |
65 | * @param addr Faulting virtual address. |
66 | * @param access Access mode that caused the fault (i.e. read/write/exec). |
66 | * @param access Access mode that caused the fault (i.e. read/write/exec). |
67 | * |
67 | * |
68 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e. serviced). |
68 | * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e. serviced). |
69 | */ |
69 | */ |
70 | int anon_page_fault(as_area_t *area, __address addr, pf_access_t access) |
70 | int anon_page_fault(as_area_t *area, __address addr, pf_access_t access) |
71 | { |
71 | { |
72 | __address frame; |
72 | __address frame; |
73 | 73 | ||
74 | if (!as_area_check_access(area, access)) |
74 | if (!as_area_check_access(area, access)) |
75 | return AS_PF_FAULT; |
75 | return AS_PF_FAULT; |
76 | 76 | ||
77 | if (area->sh_info) { |
77 | if (area->sh_info) { |
78 | btree_node_t *leaf; |
78 | btree_node_t *leaf; |
79 | 79 | ||
80 | /* |
80 | /* |
81 | * The area is shared, chances are that the mapping can be found |
81 | * The area is shared, chances are that the mapping can be found |
82 | * in the pagemap of the address space area share info structure. |
82 | * in the pagemap of the address space area share info structure. |
83 | * In the case that the pagemap does not contain the respective |
83 | * In the case that the pagemap does not contain the respective |
84 | * mapping, a new frame is allocated and the mapping is created. |
84 | * mapping, a new frame is allocated and the mapping is created. |
85 | */ |
85 | */ |
86 | mutex_lock(&area->sh_info->lock); |
86 | mutex_lock(&area->sh_info->lock); |
87 | frame = (__address) btree_search(&area->sh_info->pagemap, |
87 | frame = (__address) btree_search(&area->sh_info->pagemap, |
88 | ALIGN_DOWN(addr, PAGE_SIZE) - area->base, &leaf); |
88 | ALIGN_DOWN(addr, PAGE_SIZE) - area->base, &leaf); |
89 | if (!frame) { |
89 | if (!frame) { |
90 | bool allocate = true; |
90 | bool allocate = true; |
91 | int i; |
91 | int i; |
92 | 92 | ||
93 | /* |
93 | /* |
94 | * Zero can be returned as a valid frame address. |
94 | * Zero can be returned as a valid frame address. |
95 | * Just a small workaround. |
95 | * Just a small workaround. |
96 | */ |
96 | */ |
97 | for (i = 0; i < leaf->keys; i++) { |
97 | for (i = 0; i < leaf->keys; i++) { |
98 | if (leaf->key[i] == ALIGN_DOWN(addr, PAGE_SIZE)) { |
98 | if (leaf->key[i] == ALIGN_DOWN(addr, PAGE_SIZE)) { |
99 | allocate = false; |
99 | allocate = false; |
100 | break; |
100 | break; |
101 | } |
101 | } |
102 | } |
102 | } |
103 | if (allocate) { |
103 | if (allocate) { |
104 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
104 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
105 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
105 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
106 | 106 | ||
107 | /* |
107 | /* |
108 | * Insert the address of the newly allocated frame to the pagemap. |
108 | * Insert the address of the newly allocated frame to the pagemap. |
109 | */ |
109 | */ |
110 | btree_insert(&area->sh_info->pagemap, ALIGN_DOWN(addr, PAGE_SIZE) - area->base, (void *) frame, leaf); |
110 | btree_insert(&area->sh_info->pagemap, ALIGN_DOWN(addr, PAGE_SIZE) - area->base, (void *) frame, leaf); |
111 | } |
111 | } |
112 | } |
112 | } |
- | 113 | frame_reference_add(ADDR2PFN(frame)); |
|
113 | mutex_unlock(&area->sh_info->lock); |
114 | mutex_unlock(&area->sh_info->lock); |
114 | } else { |
115 | } else { |
115 | 116 | ||
116 | /* |
117 | /* |
117 | * In general, there can be several reasons that |
118 | * In general, there can be several reasons that |
118 | * can have caused this fault. |
119 | * can have caused this fault. |
119 | * |
120 | * |
120 | * - non-existent mapping: the area is an anonymous |
121 | * - non-existent mapping: the area is an anonymous |
121 | * area (e.g. heap or stack) and so far has not been |
122 | * area (e.g. heap or stack) and so far has not been |
122 | * allocated a frame for the faulting page |
123 | * allocated a frame for the faulting page |
123 | * |
124 | * |
124 | * - non-present mapping: another possibility, |
125 | * - non-present mapping: another possibility, |
125 | * currently not implemented, would be frame |
126 | * currently not implemented, would be frame |
126 | * reuse; when this becomes a possibility, |
127 | * reuse; when this becomes a possibility, |
127 | * do not forget to distinguish between |
128 | * do not forget to distinguish between |
128 | * the different causes |
129 | * the different causes |
129 | */ |
130 | */ |
130 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
131 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
131 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
132 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
132 | } |
133 | } |
133 | 134 | ||
134 | /* |
135 | /* |
135 | * Map 'page' to 'frame'. |
136 | * Map 'page' to 'frame'. |
136 | * Note that TLB shootdown is not attempted as only new information is being |
137 | * Note that TLB shootdown is not attempted as only new information is being |
137 | * inserted into page tables. |
138 | * inserted into page tables. |
138 | */ |
139 | */ |
139 | page_mapping_insert(AS, addr, frame, as_area_get_flags(area)); |
140 | page_mapping_insert(AS, addr, frame, as_area_get_flags(area)); |
140 | if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1)) |
141 | if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1)) |
141 | panic("Could not insert used space.\n"); |
142 | panic("Could not insert used space.\n"); |
142 | 143 | ||
143 | return AS_PF_OK; |
144 | return AS_PF_OK; |
144 | } |
145 | } |
145 | 146 | ||
146 | /** Free a frame that is backed by the anonymous memory backend. |
147 | /** Free a frame that is backed by the anonymous memory backend. |
147 | * |
148 | * |
148 | * The address space area and page tables must be already locked. |
149 | * The address space area and page tables must be already locked. |
149 | * |
150 | * |
150 | * @param area Ignored. |
151 | * @param area Ignored. |
151 | * @param page Ignored. |
152 | * @param page Ignored. |
152 | * @param frame Frame to be released. |
153 | * @param frame Frame to be released. |
153 | */ |
154 | */ |
154 | void anon_frame_free(as_area_t *area, __address page, __address frame) |
155 | void anon_frame_free(as_area_t *area, __address page, __address frame) |
155 | { |
156 | { |
156 | frame_free(ADDR2PFN(frame)); |
157 | frame_free(ADDR2PFN(frame)); |
157 | } |
158 | } |
158 | 159 | ||
159 | /** Share the anonymous address space area. |
160 | /** Share the anonymous address space area. |
160 | * |
161 | * |
161 | * Sharing of anonymous area is done by duplicating its entire mapping |
162 | * Sharing of anonymous area is done by duplicating its entire mapping |
162 | * to the pagemap. Page faults will primarily search for frames there. |
163 | * to the pagemap. Page faults will primarily search for frames there. |
163 | * |
164 | * |
164 | * The address space and address space area must be already locked. |
165 | * The address space and address space area must be already locked. |
165 | * |
166 | * |
166 | * @param area Address space area to be shared. |
167 | * @param area Address space area to be shared. |
167 | */ |
168 | */ |
168 | void anon_share(as_area_t *area) |
169 | void anon_share(as_area_t *area) |
169 | { |
170 | { |
170 | link_t *cur; |
171 | link_t *cur; |
171 | 172 | ||
172 | /* |
173 | /* |
173 | * Copy used portions of the area to sh_info's page map. |
174 | * Copy used portions of the area to sh_info's page map. |
174 | */ |
175 | */ |
175 | mutex_lock(&area->sh_info->lock); |
176 | mutex_lock(&area->sh_info->lock); |
176 | for (cur = area->used_space.leaf_head.next; cur != &area->used_space.leaf_head; cur = cur->next) { |
177 | for (cur = area->used_space.leaf_head.next; cur != &area->used_space.leaf_head; cur = cur->next) { |
177 | btree_node_t *node; |
178 | btree_node_t *node; |
178 | int i; |
179 | int i; |
179 | 180 | ||
180 | node = list_get_instance(cur, btree_node_t, leaf_link); |
181 | node = list_get_instance(cur, btree_node_t, leaf_link); |
181 | for (i = 0; i < node->keys; i++) { |
182 | for (i = 0; i < node->keys; i++) { |
182 | __address base = node->key[i]; |
183 | __address base = node->key[i]; |
183 | count_t count = (count_t) node->value[i]; |
184 | count_t count = (count_t) node->value[i]; |
184 | int j; |
185 | int j; |
185 | 186 | ||
186 | for (j = 0; j < count; j++) { |
187 | for (j = 0; j < count; j++) { |
187 | pte_t *pte; |
188 | pte_t *pte; |
188 | 189 | ||
189 | page_table_lock(area->as, false); |
190 | page_table_lock(area->as, false); |
190 | pte = page_mapping_find(area->as, base + j*PAGE_SIZE); |
191 | pte = page_mapping_find(area->as, base + j*PAGE_SIZE); |
191 | ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte)); |
192 | ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte)); |
192 | btree_insert(&area->sh_info->pagemap, (base + j*PAGE_SIZE) - area->base, |
193 | btree_insert(&area->sh_info->pagemap, (base + j*PAGE_SIZE) - area->base, |
193 | (void *) PTE_GET_FRAME(pte), NULL); |
194 | (void *) PTE_GET_FRAME(pte), NULL); |
194 | page_table_unlock(area->as, false); |
195 | page_table_unlock(area->as, false); |
- | 196 | frame_reference_add(ADDR2PFN(PTE_GET_FRAME(pte))); |
|
195 | } |
197 | } |
196 | 198 | ||
197 | } |
199 | } |
198 | } |
200 | } |
199 | mutex_unlock(&area->sh_info->lock); |
201 | mutex_unlock(&area->sh_info->lock); |
200 | } |
202 | } |
201 | 203 |