Rev 704 | Rev 718 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 704 | Rev 711 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2006 Jakub Jermar |
2 | * Copyright (C) 2001-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 | * This file contains address space manipulation functions. |
30 | * This file contains address space manipulation functions. |
31 | * Roughly speaking, this is a higher-level client of |
31 | * Roughly speaking, this is a higher-level client of |
32 | * Virtual Address Translation (VAT) subsystem. |
32 | * Virtual Address Translation (VAT) subsystem. |
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 <mm/frame.h> |
37 | #include <mm/frame.h> |
38 | #include <mm/tlb.h> |
38 | #include <mm/tlb.h> |
39 | #include <mm/heap.h> |
39 | #include <mm/heap.h> |
40 | #include <arch/mm/page.h> |
40 | #include <arch/mm/page.h> |
41 | #include <genarch/mm/page_pt.h> |
41 | #include <genarch/mm/page_pt.h> |
42 | #include <arch/mm/asid.h> |
42 | #include <arch/mm/asid.h> |
43 | #include <arch/mm/as.h> |
43 | #include <arch/mm/as.h> |
44 | #include <arch/types.h> |
44 | #include <arch/types.h> |
45 | #include <typedefs.h> |
45 | #include <typedefs.h> |
46 | #include <synch/spinlock.h> |
46 | #include <synch/spinlock.h> |
47 | #include <config.h> |
47 | #include <config.h> |
48 | #include <list.h> |
48 | #include <list.h> |
49 | #include <panic.h> |
49 | #include <panic.h> |
50 | #include <arch/asm.h> |
50 | #include <arch/asm.h> |
51 | #include <debug.h> |
51 | #include <debug.h> |
52 | #include <memstr.h> |
52 | #include <memstr.h> |
53 | #include <arch.h> |
53 | #include <arch.h> |
54 | #include <print.h> |
54 | #include <print.h> |
55 | 55 | ||
56 | #define KAS_START_INDEX PTL0_INDEX(KERNEL_ADDRESS_SPACE_START) |
56 | #define KAS_START_INDEX PTL0_INDEX(KERNEL_ADDRESS_SPACE_START) |
57 | #define KAS_END_INDEX PTL0_INDEX(KERNEL_ADDRESS_SPACE_END) |
57 | #define KAS_END_INDEX PTL0_INDEX(KERNEL_ADDRESS_SPACE_END) |
58 | #define KAS_INDICES (1+(KAS_END_INDEX-KAS_START_INDEX)) |
58 | #define KAS_INDICES (1+(KAS_END_INDEX-KAS_START_INDEX)) |
59 | 59 | ||
60 | /* |
60 | /* |
61 | * Here we assume that PFN (Physical Frame Number) space |
61 | * Here we assume that PFN (Physical Frame Number) space |
62 | * is smaller than the width of index_t. UNALLOCATED_PFN |
62 | * is smaller than the width of index_t. UNALLOCATED_PFN |
63 | * can be then used to mark mappings wich were not |
63 | * can be then used to mark mappings wich were not |
64 | * yet allocated a physical frame. |
64 | * yet allocated a physical frame. |
65 | */ |
65 | */ |
66 | #define UNALLOCATED_PFN ((index_t) -1) |
66 | #define UNALLOCATED_PFN ((index_t) -1) |
67 | 67 | ||
68 | /** Create address space. */ |
68 | /** Create address space. */ |
69 | /* |
69 | /* |
70 | * FIXME: this interface must be meaningful for all possible VAT |
70 | * FIXME: this interface must be meaningful for all possible VAT |
71 | * (Virtual Address Translation) mechanisms. |
71 | * (Virtual Address Translation) mechanisms. |
72 | */ |
72 | */ |
73 | as_t *as_create(pte_t *ptl0) |
73 | as_t *as_create(pte_t *ptl0) |
74 | { |
74 | { |
75 | as_t *as; |
75 | as_t *as; |
76 | 76 | ||
77 | as = (as_t *) malloc(sizeof(as_t)); |
77 | as = (as_t *) malloc(sizeof(as_t)); |
78 | if (as) { |
78 | if (as) { |
79 | spinlock_initialize(&as->lock, "as_lock"); |
79 | spinlock_initialize(&as->lock, "as_lock"); |
80 | list_initialize(&as->as_area_head); |
80 | list_initialize(&as->as_area_head); |
81 | 81 | ||
82 | as->asid = asid_get(); |
82 | as->asid = asid_get(); |
83 | 83 | ||
84 | as->ptl0 = ptl0; |
84 | as->ptl0 = ptl0; |
85 | if (!as->ptl0) { |
85 | if (!as->ptl0) { |
86 | pte_t *src_ptl0, *dst_ptl0; |
86 | pte_t *src_ptl0, *dst_ptl0; |
87 | 87 | ||
88 | src_ptl0 = (pte_t *) PA2KA((__address) GET_PTL0_ADDRESS()); |
88 | src_ptl0 = (pte_t *) PA2KA((__address) GET_PTL0_ADDRESS()); |
89 | dst_ptl0 = (pte_t *) frame_alloc(FRAME_KA | FRAME_PANIC, ONE_FRAME, NULL); |
89 | dst_ptl0 = (pte_t *) frame_alloc(FRAME_KA | FRAME_PANIC, ONE_FRAME, NULL); |
90 | 90 | ||
91 | // memsetb((__address) dst_ptl0, PAGE_SIZE, 0); |
91 | // memsetb((__address) dst_ptl0, PAGE_SIZE, 0); |
92 | // memcpy((void *) &dst_ptl0[KAS_START_INDEX], (void *) &src_ptl0[KAS_START_INDEX], KAS_INDICES); |
92 | // memcpy((void *) &dst_ptl0[KAS_START_INDEX], (void *) &src_ptl0[KAS_START_INDEX], KAS_INDICES); |
93 | 93 | ||
94 | memcpy((void *) dst_ptl0,(void *) src_ptl0, PAGE_SIZE); |
94 | memcpy((void *) dst_ptl0,(void *) src_ptl0, PAGE_SIZE); |
95 | 95 | ||
96 | as->ptl0 = (pte_t *) KA2PA((__address) dst_ptl0); |
96 | as->ptl0 = (pte_t *) KA2PA((__address) dst_ptl0); |
97 | } |
97 | } |
98 | } |
98 | } |
99 | 99 | ||
100 | return as; |
100 | return as; |
101 | } |
101 | } |
102 | 102 | ||
103 | /** Create address space area of common attributes. |
103 | /** Create address space area of common attributes. |
104 | * |
104 | * |
105 | * The created address space area is added to the target address space. |
105 | * The created address space area is added to the target address space. |
106 | * |
106 | * |
107 | * @param as Target address space. |
107 | * @param as Target address space. |
108 | * @param type Type of area. |
108 | * @param type Type of area. |
109 | * @param size Size of area in multiples of PAGE_SIZE. |
109 | * @param size Size of area in multiples of PAGE_SIZE. |
110 | * @param base Base address of area. |
110 | * @param base Base address of area. |
111 | * |
111 | * |
112 | * @return Address space area on success or NULL on failure. |
112 | * @return Address space area on success or NULL on failure. |
113 | */ |
113 | */ |
114 | as_area_t *as_area_create(as_t *as, as_area_type_t type, size_t size, __address base) |
114 | as_area_t *as_area_create(as_t *as, as_area_type_t type, size_t size, __address base) |
115 | { |
115 | { |
116 | ipl_t ipl; |
116 | ipl_t ipl; |
117 | as_area_t *a; |
117 | as_area_t *a; |
118 | 118 | ||
119 | if (base % PAGE_SIZE) |
119 | if (base % PAGE_SIZE) |
120 | panic("addr not aligned to a page boundary"); |
120 | panic("addr not aligned to a page boundary"); |
121 | 121 | ||
122 | ipl = interrupts_disable(); |
122 | ipl = interrupts_disable(); |
123 | spinlock_lock(&as->lock); |
123 | spinlock_lock(&as->lock); |
124 | 124 | ||
125 | /* |
125 | /* |
126 | * TODO: test as_area which is to be created doesn't overlap with an existing one. |
126 | * TODO: test as_area which is to be created doesn't overlap with an existing one. |
127 | */ |
127 | */ |
128 | 128 | ||
129 | a = (as_area_t *) malloc(sizeof(as_area_t)); |
129 | a = (as_area_t *) malloc(sizeof(as_area_t)); |
130 | if (a) { |
130 | if (a) { |
131 | int i; |
131 | int i; |
132 | 132 | ||
133 | a->mapping = (index_t *) malloc(size * sizeof(index_t)); |
133 | a->mapping = (index_t *) malloc(size * sizeof(index_t)); |
134 | if (!a->mapping) { |
134 | if (!a->mapping) { |
135 | free(a); |
135 | free(a); |
136 | spinlock_unlock(&as->lock); |
136 | spinlock_unlock(&as->lock); |
137 | interrupts_restore(ipl); |
137 | interrupts_restore(ipl); |
138 | return NULL; |
138 | return NULL; |
139 | } |
139 | } |
140 | 140 | ||
141 | for (i=0; i<size; i++) { |
141 | for (i=0; i<size; i++) { |
142 | /* |
142 | /* |
143 | * Frames will be allocated on-demand by |
143 | * Frames will be allocated on-demand by |
144 | * as_page_fault() or preloaded by |
144 | * as_page_fault() or preloaded by |
145 | * as_area_load_mapping(). |
145 | * as_area_load_mapping(). |
146 | */ |
146 | */ |
147 | a->mapping[i] = UNALLOCATED_PFN; |
147 | a->mapping[i] = UNALLOCATED_PFN; |
148 | } |
148 | } |
149 | 149 | ||
150 | spinlock_initialize(&a->lock, "as_area_lock"); |
150 | spinlock_initialize(&a->lock, "as_area_lock"); |
151 | 151 | ||
152 | link_initialize(&a->link); |
152 | link_initialize(&a->link); |
153 | a->type = type; |
153 | a->type = type; |
154 | a->size = size; |
154 | a->size = size; |
155 | a->base = base; |
155 | a->base = base; |
156 | 156 | ||
157 | list_append(&a->link, &as->as_area_head); |
157 | list_append(&a->link, &as->as_area_head); |
158 | 158 | ||
159 | } |
159 | } |
160 | 160 | ||
161 | spinlock_unlock(&as->lock); |
161 | spinlock_unlock(&as->lock); |
162 | interrupts_restore(ipl); |
162 | interrupts_restore(ipl); |
163 | 163 | ||
164 | return a; |
164 | return a; |
165 | } |
165 | } |
166 | 166 | ||
167 | /** Load mapping for address space area. |
167 | /** Load mapping for address space area. |
168 | * |
168 | * |
169 | * Initialize a->mapping. |
169 | * Initialize a->mapping. |
170 | * |
170 | * |
171 | * @param a Target address space area. |
171 | * @param a Target address space area. |
172 | * @param pfn Array of frame numbers. Number of elements must match with a->mapping. |
172 | * @param pfn Array of frame numbers. Number of elements must match with a->mapping. |
173 | */ |
173 | */ |
174 | void as_area_load_mapping(as_area_t *a, index_t *pfn) |
174 | void as_area_load_mapping(as_area_t *a, index_t *pfn) |
175 | { |
175 | { |
176 | ipl_t ipl; |
176 | ipl_t ipl; |
177 | int i; |
177 | int i; |
178 | 178 | ||
179 | ipl = interrupts_disable(); |
179 | ipl = interrupts_disable(); |
180 | spinlock_lock(&a->lock); |
180 | spinlock_lock(&a->lock); |
181 | 181 | ||
182 | for (i = 0; i < a->size; i++) { |
182 | for (i = 0; i < a->size; i++) { |
183 | ASSERT(a->mapping[i] == UNALLOCATED_PFN); |
183 | ASSERT(a->mapping[i] == UNALLOCATED_PFN); |
184 | ASSERT(pfn[i] != UNALLOCATED_PFN); |
184 | ASSERT(pfn[i] != UNALLOCATED_PFN); |
185 | a->mapping[i] = pfn[i]; |
185 | a->mapping[i] = pfn[i]; |
186 | } |
186 | } |
187 | 187 | ||
188 | spinlock_unlock(&a->lock); |
188 | spinlock_unlock(&a->lock); |
189 | interrupts_restore(ipl); |
189 | interrupts_restore(ipl); |
190 | } |
190 | } |
191 | 191 | ||
192 | /** Handle page fault within the current address space. |
192 | /** Handle page fault within the current address space. |
193 | * |
193 | * |
194 | * This is the high-level page fault handler. |
194 | * This is the high-level page fault handler. |
195 | * Interrupts are assumed disabled. |
195 | * Interrupts are assumed disabled. |
196 | * |
196 | * |
197 | * @param page Faulting page. |
197 | * @param page Faulting page. |
198 | * |
198 | * |
199 | * @return 0 on page fault, 1 on success. |
199 | * @return 0 on page fault, 1 on success. |
200 | */ |
200 | */ |
201 | int as_page_fault(__address page) |
201 | int as_page_fault(__address page) |
202 | { |
202 | { |
203 | int flags; |
203 | int flags; |
204 | link_t *cur; |
204 | link_t *cur; |
205 | as_area_t *a, *area = NULL; |
205 | as_area_t *a, *area = NULL; |
206 | index_t vpn; |
206 | index_t vpn; |
207 | __address frame; |
207 | __address frame; |
208 | 208 | ||
209 | ASSERT(AS); |
209 | ASSERT(AS); |
210 | spinlock_lock(&AS->lock); |
210 | spinlock_lock(&AS->lock); |
211 | 211 | ||
212 | /* |
212 | /* |
213 | * Search this areas of this address space for presence of 'page'. |
213 | * Search this areas of this address space for presence of 'page'. |
214 | */ |
214 | */ |
215 | for (cur = AS->as_area_head.next; cur != &AS->as_area_head; cur = cur->next) { |
215 | for (cur = AS->as_area_head.next; cur != &AS->as_area_head; cur = cur->next) { |
216 | a = list_get_instance(cur, as_area_t, link); |
216 | a = list_get_instance(cur, as_area_t, link); |
217 | spinlock_lock(&a->lock); |
217 | spinlock_lock(&a->lock); |
218 | 218 | ||
219 | if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) { |
219 | if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) { |
220 | 220 | ||
221 | /* |
221 | /* |
222 | * We found the area containing 'page'. |
222 | * We found the area containing 'page'. |
223 | * TODO: access checking |
223 | * TODO: access checking |
224 | */ |
224 | */ |
225 | 225 | ||
226 | vpn = (page - a->base) / PAGE_SIZE; |
226 | vpn = (page - a->base) / PAGE_SIZE; |
227 | area = a; |
227 | area = a; |
228 | break; |
228 | break; |
229 | } |
229 | } |
230 | 230 | ||
231 | spinlock_unlock(&a->lock); |
231 | spinlock_unlock(&a->lock); |
232 | } |
232 | } |
233 | 233 | ||
234 | if (!area) { |
234 | if (!area) { |
235 | /* |
235 | /* |
236 | * No area contained mapping for 'page'. |
236 | * No area contained mapping for 'page'. |
237 | * Signal page fault to low-level handler. |
237 | * Signal page fault to low-level handler. |
238 | */ |
238 | */ |
239 | spinlock_unlock(&AS->lock); |
239 | spinlock_unlock(&AS->lock); |
240 | return 0; |
240 | return 0; |
241 | } |
241 | } |
242 | 242 | ||
243 | /* |
243 | /* |
244 | * Note: area->lock is held. |
244 | * Note: area->lock is held. |
245 | */ |
245 | */ |
246 | 246 | ||
247 | /* |
247 | /* |
248 | * Decide if a frame needs to be allocated. |
248 | * Decide if a frame needs to be allocated. |
249 | * If so, allocate it and adjust area->mapping map. |
249 | * If so, allocate it and adjust area->mapping map. |
250 | */ |
250 | */ |
251 | if (area->mapping[vpn] == UNALLOCATED_PFN) { |
251 | if (area->mapping[vpn] == UNALLOCATED_PFN) { |
252 | frame = frame_alloc(0, ONE_FRAME, NULL); |
252 | frame = frame_alloc(0, ONE_FRAME, NULL); |
253 | memsetb(frame, FRAME_SIZE, 0); |
253 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
254 | area->mapping[vpn] = frame / FRAME_SIZE; |
254 | area->mapping[vpn] = frame / FRAME_SIZE; |
255 | ASSERT(area->mapping[vpn] != UNALLOCATED_PFN); |
255 | ASSERT(area->mapping[vpn] != UNALLOCATED_PFN); |
256 | } else { |
256 | } else { |
257 | frame = area->mapping[vpn] * FRAME_SIZE; |
257 | frame = area->mapping[vpn] * FRAME_SIZE; |
258 | } |
258 | } |
259 | 259 | ||
260 | switch (area->type) { |
260 | switch (area->type) { |
261 | case AS_AREA_TEXT: |
261 | case AS_AREA_TEXT: |
262 | flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
262 | flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
263 | break; |
263 | break; |
264 | case AS_AREA_DATA: |
264 | case AS_AREA_DATA: |
265 | case AS_AREA_STACK: |
265 | case AS_AREA_STACK: |
266 | flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
266 | flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
267 | break; |
267 | break; |
268 | default: |
268 | default: |
269 | panic("unexpected as_area_type_t %d", area->type); |
269 | panic("unexpected as_area_type_t %d", area->type); |
270 | } |
270 | } |
271 | 271 | ||
272 | /* |
272 | /* |
273 | * Map 'page' to 'frame'. |
273 | * Map 'page' to 'frame'. |
274 | * Note that TLB shootdown is not attempted as only new information is being |
274 | * Note that TLB shootdown is not attempted as only new information is being |
275 | * inserted into page tables. |
275 | * inserted into page tables. |
276 | */ |
276 | */ |
277 | page_mapping_insert(page, AS->asid, frame, flags, (__address) AS->ptl0); |
277 | page_mapping_insert(page, AS->asid, frame, flags, (__address) AS->ptl0); |
278 | 278 | ||
279 | spinlock_unlock(&area->lock); |
279 | spinlock_unlock(&area->lock); |
280 | spinlock_unlock(&AS->lock); |
280 | spinlock_unlock(&AS->lock); |
281 | 281 | ||
282 | return 1; |
282 | return 1; |
283 | } |
283 | } |
284 | 284 | ||
285 | /** Install address space on CPU. |
285 | /** Install address space on CPU. |
286 | * |
286 | * |
287 | * @param as Address space. |
287 | * @param as Address space. |
288 | */ |
288 | */ |
289 | void as_install(as_t *as) |
289 | void as_install(as_t *as) |
290 | { |
290 | { |
291 | ipl_t ipl; |
291 | ipl_t ipl; |
292 | 292 | ||
293 | ipl = interrupts_disable(); |
293 | ipl = interrupts_disable(); |
294 | spinlock_lock(&as->lock); |
294 | spinlock_lock(&as->lock); |
295 | ASSERT(as->ptl0); |
295 | ASSERT(as->ptl0); |
296 | SET_PTL0_ADDRESS(as->ptl0); |
296 | SET_PTL0_ADDRESS(as->ptl0); |
297 | spinlock_unlock(&as->lock); |
297 | spinlock_unlock(&as->lock); |
298 | interrupts_restore(ipl); |
298 | interrupts_restore(ipl); |
299 | 299 | ||
300 | /* |
300 | /* |
301 | * Perform architecture-specific steps. |
301 | * Perform architecture-specific steps. |
302 | * (e.g. invalidate TLB, install ASID etc.) |
302 | * (e.g. invalidate TLB, install ASID etc.) |
303 | */ |
303 | */ |
304 | as_install_arch(as); |
304 | as_install_arch(as); |
305 | 305 | ||
306 | AS = as; |
306 | AS = as; |
307 | } |
307 | } |
308 | 308 |