Rev 1235 | Rev 1238 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1235 | Rev 1236 | ||
---|---|---|---|
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 <arch/mm/as.h> |
36 | #include <arch/mm/as.h> |
37 | #include <mm/page.h> |
37 | #include <mm/page.h> |
38 | #include <mm/frame.h> |
38 | #include <mm/frame.h> |
39 | #include <mm/slab.h> |
39 | #include <mm/slab.h> |
40 | #include <mm/tlb.h> |
40 | #include <mm/tlb.h> |
41 | #include <arch/mm/page.h> |
41 | #include <arch/mm/page.h> |
42 | #include <genarch/mm/page_pt.h> |
42 | #include <genarch/mm/page_pt.h> |
43 | #include <genarch/mm/page_ht.h> |
43 | #include <genarch/mm/page_ht.h> |
44 | #include <mm/asid.h> |
44 | #include <mm/asid.h> |
45 | #include <arch/mm/asid.h> |
45 | #include <arch/mm/asid.h> |
46 | #include <synch/spinlock.h> |
46 | #include <synch/spinlock.h> |
47 | #include <adt/list.h> |
47 | #include <adt/list.h> |
48 | #include <adt/btree.h> |
48 | #include <adt/btree.h> |
49 | #include <proc/task.h> |
49 | #include <proc/task.h> |
50 | #include <arch/asm.h> |
50 | #include <arch/asm.h> |
51 | #include <panic.h> |
51 | #include <panic.h> |
52 | #include <debug.h> |
52 | #include <debug.h> |
53 | #include <print.h> |
53 | #include <print.h> |
54 | #include <memstr.h> |
54 | #include <memstr.h> |
55 | #include <macros.h> |
55 | #include <macros.h> |
56 | #include <arch.h> |
56 | #include <arch.h> |
57 | #include <errno.h> |
57 | #include <errno.h> |
58 | #include <config.h> |
58 | #include <config.h> |
59 | #include <arch/types.h> |
59 | #include <arch/types.h> |
60 | #include <typedefs.h> |
60 | #include <typedefs.h> |
61 | 61 | ||
62 | as_operations_t *as_operations = NULL; |
62 | as_operations_t *as_operations = NULL; |
63 | 63 | ||
64 | /** Address space lock. It protects inactive_as_with_asid_head. */ |
64 | /** Address space lock. It protects inactive_as_with_asid_head. */ |
65 | SPINLOCK_INITIALIZE(as_lock); |
65 | SPINLOCK_INITIALIZE(as_lock); |
66 | 66 | ||
67 | /** |
67 | /** |
68 | * This list contains address spaces that are not active on any |
68 | * This list contains address spaces that are not active on any |
69 | * processor and that have valid ASID. |
69 | * processor and that have valid ASID. |
70 | */ |
70 | */ |
71 | LIST_INITIALIZE(inactive_as_with_asid_head); |
71 | LIST_INITIALIZE(inactive_as_with_asid_head); |
72 | 72 | ||
73 | /** Kernel address space. */ |
73 | /** Kernel address space. */ |
74 | as_t *AS_KERNEL = NULL; |
74 | as_t *AS_KERNEL = NULL; |
75 | 75 | ||
76 | static int area_flags_to_page_flags(int aflags); |
76 | static int area_flags_to_page_flags(int aflags); |
77 | static int get_area_flags(as_area_t *a); |
77 | static int get_area_flags(as_area_t *a); |
78 | static as_area_t *find_area_and_lock(as_t *as, __address va); |
78 | static as_area_t *find_area_and_lock(as_t *as, __address va); |
79 | static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area); |
79 | static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area); |
80 | 80 | ||
81 | /** Initialize address space subsystem. */ |
81 | /** Initialize address space subsystem. */ |
82 | void as_init(void) |
82 | void as_init(void) |
83 | { |
83 | { |
84 | as_arch_init(); |
84 | as_arch_init(); |
85 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
85 | AS_KERNEL = as_create(FLAG_AS_KERNEL); |
86 | if (!AS_KERNEL) |
86 | if (!AS_KERNEL) |
87 | panic("can't create kernel address space\n"); |
87 | panic("can't create kernel address space\n"); |
88 | } |
88 | } |
89 | 89 | ||
90 | /** Create address space. |
90 | /** Create address space. |
91 | * |
91 | * |
92 | * @param flags Flags that influence way in wich the address space is created. |
92 | * @param flags Flags that influence way in wich the address space is created. |
93 | */ |
93 | */ |
94 | as_t *as_create(int flags) |
94 | as_t *as_create(int flags) |
95 | { |
95 | { |
96 | as_t *as; |
96 | as_t *as; |
97 | 97 | ||
98 | as = (as_t *) malloc(sizeof(as_t), 0); |
98 | as = (as_t *) malloc(sizeof(as_t), 0); |
99 | link_initialize(&as->inactive_as_with_asid_link); |
99 | link_initialize(&as->inactive_as_with_asid_link); |
100 | spinlock_initialize(&as->lock, "as_lock"); |
100 | spinlock_initialize(&as->lock, "as_lock"); |
101 | btree_create(&as->as_area_btree); |
101 | btree_create(&as->as_area_btree); |
102 | 102 | ||
103 | if (flags & FLAG_AS_KERNEL) |
103 | if (flags & FLAG_AS_KERNEL) |
104 | as->asid = ASID_KERNEL; |
104 | as->asid = ASID_KERNEL; |
105 | else |
105 | else |
106 | as->asid = ASID_INVALID; |
106 | as->asid = ASID_INVALID; |
107 | 107 | ||
108 | as->refcount = 0; |
108 | as->refcount = 0; |
109 | as->page_table = page_table_create(flags); |
109 | as->page_table = page_table_create(flags); |
110 | 110 | ||
111 | return as; |
111 | return as; |
112 | } |
112 | } |
113 | 113 | ||
114 | /** Free Adress space */ |
114 | /** Free Adress space */ |
115 | void as_free(as_t *as) |
115 | void as_free(as_t *as) |
116 | { |
116 | { |
117 | ASSERT(as->refcount == 0); |
117 | ASSERT(as->refcount == 0); |
118 | 118 | ||
119 | /* TODO: free as_areas and other resources held by as */ |
119 | /* TODO: free as_areas and other resources held by as */ |
120 | /* TODO: free page table */ |
120 | /* TODO: free page table */ |
121 | free(as); |
121 | free(as); |
122 | } |
122 | } |
123 | 123 | ||
124 | /** Create address space area of common attributes. |
124 | /** Create address space area of common attributes. |
125 | * |
125 | * |
126 | * The created address space area is added to the target address space. |
126 | * The created address space area is added to the target address space. |
127 | * |
127 | * |
128 | * @param as Target address space. |
128 | * @param as Target address space. |
129 | * @param flags Flags of the area. |
129 | * @param flags Flags of the area. |
130 | * @param size Size of area. |
130 | * @param size Size of area. |
131 | * @param base Base address of area. |
131 | * @param base Base address of area. |
132 | * |
132 | * |
133 | * @return Address space area on success or NULL on failure. |
133 | * @return Address space area on success or NULL on failure. |
134 | */ |
134 | */ |
135 | as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base) |
135 | as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base) |
136 | { |
136 | { |
137 | ipl_t ipl; |
137 | ipl_t ipl; |
138 | as_area_t *a; |
138 | as_area_t *a; |
139 | 139 | ||
140 | if (base % PAGE_SIZE) |
140 | if (base % PAGE_SIZE) |
141 | return NULL; |
141 | return NULL; |
142 | 142 | ||
143 | if (!size) |
143 | if (!size) |
144 | return NULL; |
144 | return NULL; |
145 | 145 | ||
146 | /* Writeable executable areas are not supported. */ |
146 | /* Writeable executable areas are not supported. */ |
147 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE)) |
147 | if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE)) |
148 | return NULL; |
148 | return NULL; |
149 | 149 | ||
150 | ipl = interrupts_disable(); |
150 | ipl = interrupts_disable(); |
151 | spinlock_lock(&as->lock); |
151 | spinlock_lock(&as->lock); |
152 | 152 | ||
153 | if (!check_area_conflicts(as, base, size, NULL)) { |
153 | if (!check_area_conflicts(as, base, size, NULL)) { |
154 | spinlock_unlock(&as->lock); |
154 | spinlock_unlock(&as->lock); |
155 | interrupts_restore(ipl); |
155 | interrupts_restore(ipl); |
156 | return NULL; |
156 | return NULL; |
157 | } |
157 | } |
158 | 158 | ||
159 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
159 | a = (as_area_t *) malloc(sizeof(as_area_t), 0); |
160 | 160 | ||
161 | spinlock_initialize(&a->lock, "as_area_lock"); |
161 | spinlock_initialize(&a->lock, "as_area_lock"); |
162 | 162 | ||
163 | a->flags = flags; |
163 | a->flags = flags; |
164 | a->pages = SIZE2FRAMES(size); |
164 | a->pages = SIZE2FRAMES(size); |
165 | a->base = base; |
165 | a->base = base; |
166 | 166 | ||
167 | btree_insert(&as->as_area_btree, base, (void *) a, NULL); |
167 | btree_insert(&as->as_area_btree, base, (void *) a, NULL); |
168 | 168 | ||
169 | spinlock_unlock(&as->lock); |
169 | spinlock_unlock(&as->lock); |
170 | interrupts_restore(ipl); |
170 | interrupts_restore(ipl); |
171 | 171 | ||
172 | return a; |
172 | return a; |
173 | } |
173 | } |
174 | 174 | ||
175 | /** Find address space area and change it. |
175 | /** Find address space area and change it. |
176 | * |
176 | * |
177 | * @param as Address space. |
177 | * @param as Address space. |
178 | * @param address Virtual address belonging to the area to be changed. Must be page-aligned. |
178 | * @param address Virtual address belonging to the area to be changed. Must be page-aligned. |
179 | * @param size New size of the virtual memory block starting at address. |
179 | * @param size New size of the virtual memory block starting at address. |
180 | * @param flags Flags influencing the remap operation. Currently unused. |
180 | * @param flags Flags influencing the remap operation. Currently unused. |
181 | * |
181 | * |
182 | * @return address on success, (__address) -1 otherwise. |
182 | * @return address on success, (__address) -1 otherwise. |
183 | */ |
183 | */ |
184 | __address as_area_resize(as_t *as, __address address, size_t size, int flags) |
184 | __address as_area_resize(as_t *as, __address address, size_t size, int flags) |
185 | { |
185 | { |
186 | as_area_t *area = NULL; |
186 | as_area_t *area = NULL; |
187 | ipl_t ipl; |
187 | ipl_t ipl; |
188 | size_t pages; |
188 | size_t pages; |
189 | 189 | ||
190 | ipl = interrupts_disable(); |
190 | ipl = interrupts_disable(); |
191 | spinlock_lock(&as->lock); |
191 | spinlock_lock(&as->lock); |
192 | 192 | ||
193 | /* |
193 | /* |
194 | * Locate the area. |
194 | * Locate the area. |
195 | */ |
195 | */ |
196 | area = find_area_and_lock(as, address); |
196 | area = find_area_and_lock(as, address); |
197 | if (!area) { |
197 | if (!area) { |
198 | spinlock_unlock(&as->lock); |
198 | spinlock_unlock(&as->lock); |
199 | interrupts_restore(ipl); |
199 | interrupts_restore(ipl); |
200 | return (__address) -1; |
200 | return (__address) -1; |
201 | } |
201 | } |
202 | 202 | ||
203 | if (area->flags & AS_AREA_DEVICE) { |
203 | if (area->flags & AS_AREA_DEVICE) { |
204 | /* |
204 | /* |
205 | * Remapping of address space areas associated |
205 | * Remapping of address space areas associated |
206 | * with memory mapped devices is not supported. |
206 | * with memory mapped devices is not supported. |
207 | */ |
207 | */ |
208 | spinlock_unlock(&area->lock); |
208 | spinlock_unlock(&area->lock); |
209 | spinlock_unlock(&as->lock); |
209 | spinlock_unlock(&as->lock); |
210 | interrupts_restore(ipl); |
210 | interrupts_restore(ipl); |
211 | return (__address) -1; |
211 | return (__address) -1; |
212 | } |
212 | } |
213 | 213 | ||
214 | pages = SIZE2FRAMES((address - area->base) + size); |
214 | pages = SIZE2FRAMES((address - area->base) + size); |
215 | if (!pages) { |
215 | if (!pages) { |
216 | /* |
216 | /* |
217 | * Zero size address space areas are not allowed. |
217 | * Zero size address space areas are not allowed. |
218 | */ |
218 | */ |
219 | spinlock_unlock(&area->lock); |
219 | spinlock_unlock(&area->lock); |
220 | spinlock_unlock(&as->lock); |
220 | spinlock_unlock(&as->lock); |
221 | interrupts_restore(ipl); |
221 | interrupts_restore(ipl); |
222 | return (__address) -1; |
222 | return (__address) -1; |
223 | } |
223 | } |
224 | 224 | ||
225 | if (pages < area->pages) { |
225 | if (pages < area->pages) { |
226 | int i; |
226 | int i; |
227 | 227 | ||
228 | /* |
228 | /* |
229 | * Shrinking the area. |
229 | * Shrinking the area. |
230 | * No need to check for overlaps. |
230 | * No need to check for overlaps. |
231 | */ |
231 | */ |
232 | for (i = pages; i < area->pages; i++) { |
232 | for (i = pages; i < area->pages; i++) { |
233 | pte_t *pte; |
233 | pte_t *pte; |
234 | 234 | ||
235 | /* |
235 | /* |
236 | * Releasing physical memory. |
236 | * Releasing physical memory. |
237 | * This depends on the fact that the memory was allocated using frame_alloc(). |
237 | * This depends on the fact that the memory was allocated using frame_alloc(). |
238 | */ |
238 | */ |
239 | page_table_lock(as, false); |
239 | page_table_lock(as, false); |
240 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
240 | pte = page_mapping_find(as, area->base + i*PAGE_SIZE); |
241 | if (pte && PTE_VALID(pte)) { |
241 | if (pte && PTE_VALID(pte)) { |
242 | __address frame; |
242 | __address frame; |
243 | 243 | ||
244 | ASSERT(PTE_PRESENT(pte)); |
244 | ASSERT(PTE_PRESENT(pte)); |
245 | frame = PTE_GET_FRAME(pte); |
245 | frame = PTE_GET_FRAME(pte); |
246 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
246 | page_mapping_remove(as, area->base + i*PAGE_SIZE); |
247 | page_table_unlock(as, false); |
247 | page_table_unlock(as, false); |
248 | 248 | ||
249 | frame_free(ADDR2PFN(frame)); |
249 | frame_free(ADDR2PFN(frame)); |
250 | } else { |
250 | } else { |
251 | page_table_unlock(as, false); |
251 | page_table_unlock(as, false); |
252 | } |
252 | } |
253 | } |
253 | } |
254 | /* |
254 | /* |
255 | * Invalidate TLB's. |
255 | * Invalidate TLB's. |
256 | */ |
256 | */ |
257 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages); |
257 | tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages); |
258 | tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages); |
258 | tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages); |
259 | tlb_shootdown_finalize(); |
259 | tlb_shootdown_finalize(); |
260 | } else { |
260 | } else { |
261 | /* |
261 | /* |
262 | * Growing the area. |
262 | * Growing the area. |
263 | * Check for overlaps with other address space areas. |
263 | * Check for overlaps with other address space areas. |
264 | */ |
264 | */ |
265 | if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) { |
265 | if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) { |
266 | spinlock_unlock(&area->lock); |
266 | spinlock_unlock(&area->lock); |
267 | spinlock_unlock(&as->lock); |
267 | spinlock_unlock(&as->lock); |
268 | interrupts_restore(ipl); |
268 | interrupts_restore(ipl); |
269 | return (__address) -1; |
269 | return (__address) -1; |
270 | } |
270 | } |
271 | } |
271 | } |
272 | 272 | ||
273 | area->pages = pages; |
273 | area->pages = pages; |
274 | 274 | ||
275 | spinlock_unlock(&area->lock); |
275 | spinlock_unlock(&area->lock); |
276 | spinlock_unlock(&as->lock); |
276 | spinlock_unlock(&as->lock); |
277 | interrupts_restore(ipl); |
277 | interrupts_restore(ipl); |
278 | 278 | ||
279 | return address; |
279 | return address; |
280 | } |
280 | } |
281 | 281 | ||
282 | /** Send address space area to another task. |
282 | /** Send address space area to another task. |
283 | * |
283 | * |
284 | * Address space area is sent to the specified task. |
284 | * Address space area is sent to the specified task. |
285 | * If the destination task is willing to accept the |
285 | * If the destination task is willing to accept the |
286 | * area, a new area is created according to the |
286 | * area, a new area is created according to the |
287 | * source area. Moreover, any existing mapping |
287 | * source area. Moreover, any existing mapping |
288 | * is copied as well, providing thus a mechanism |
288 | * is copied as well, providing thus a mechanism |
289 | * for sharing group of pages. The source address |
289 | * for sharing group of pages. The source address |
290 | * space area and any associated mapping is preserved. |
290 | * space area and any associated mapping is preserved. |
291 | * |
291 | * |
292 | * @param id Task ID of the accepting task. |
292 | * @param id Task ID of the accepting task. |
293 | * @param base Base address of the source address space area. |
293 | * @param base Base address of the source address space area. |
294 | * @param size Size of the source address space area. |
294 | * @param size Size of the source address space area. |
295 | * @param flags Flags of the source address space area. |
295 | * @param flags Flags of the source address space area. |
296 | * |
296 | * |
297 | * @return 0 on success or ENOENT if there is no such task or |
297 | * @return 0 on success or ENOENT if there is no such task or |
298 | * if there is no such address space area, |
298 | * if there is no such address space area, |
299 | * EPERM if there was a problem in accepting the area or |
299 | * EPERM if there was a problem in accepting the area or |
300 | * ENOMEM if there was a problem in allocating destination |
300 | * ENOMEM if there was a problem in allocating destination |
301 | * address space area. |
301 | * address space area. |
302 | */ |
302 | */ |
303 | int as_area_send(task_id_t id, __address base, size_t size, int flags) |
303 | int as_area_send(task_id_t id, __address base, size_t size, int flags) |
304 | { |
304 | { |
305 | ipl_t ipl; |
305 | ipl_t ipl; |
306 | task_t *t; |
306 | task_t *t; |
307 | count_t i; |
307 | count_t i; |
308 | as_t *as; |
308 | as_t *as; |
309 | __address dst_base; |
309 | __address dst_base; |
310 | 310 | ||
311 | ipl = interrupts_disable(); |
311 | ipl = interrupts_disable(); |
312 | spinlock_lock(&tasks_lock); |
312 | spinlock_lock(&tasks_lock); |
313 | 313 | ||
314 | t = task_find_by_id(id); |
314 | t = task_find_by_id(id); |
315 | if (!NULL) { |
315 | if (!NULL) { |
316 | spinlock_unlock(&tasks_lock); |
316 | spinlock_unlock(&tasks_lock); |
317 | interrupts_restore(ipl); |
317 | interrupts_restore(ipl); |
318 | return ENOENT; |
318 | return ENOENT; |
319 | } |
319 | } |
320 | 320 | ||
321 | spinlock_lock(&t->lock); |
321 | spinlock_lock(&t->lock); |
322 | spinlock_unlock(&tasks_lock); |
322 | spinlock_unlock(&tasks_lock); |
323 | 323 | ||
324 | as = t->as; |
324 | as = t->as; |
325 | dst_base = (__address) t->accept_arg.base; |
325 | dst_base = (__address) t->accept_arg.base; |
326 | 326 | ||
327 | if (as == AS) { |
327 | if (as == AS) { |
328 | /* |
328 | /* |
329 | * The two tasks share the entire address space. |
329 | * The two tasks share the entire address space. |
330 | * Return error since there is no point in continuing. |
330 | * Return error since there is no point in continuing. |
331 | */ |
331 | */ |
332 | spinlock_unlock(&t->lock); |
332 | spinlock_unlock(&t->lock); |
333 | interrupts_restore(ipl); |
333 | interrupts_restore(ipl); |
334 | return EPERM; |
334 | return EPERM; |
335 | } |
335 | } |
336 | 336 | ||
337 | if ((t->accept_arg.task_id != TASK->taskid) || (t->accept_arg.size != size) || |
337 | if ((t->accept_arg.task_id != TASK->taskid) || (t->accept_arg.size != size) || |
338 | (t->accept_arg.flags != flags)) { |
338 | (t->accept_arg.flags != flags)) { |
339 | /* |
339 | /* |
340 | * Discrepancy in either task ID, size or flags. |
340 | * Discrepancy in either task ID, size or flags. |
341 | */ |
341 | */ |
342 | spinlock_unlock(&t->lock); |
342 | spinlock_unlock(&t->lock); |
343 | interrupts_restore(ipl); |
343 | interrupts_restore(ipl); |
344 | return EPERM; |
344 | return EPERM; |
345 | } |
345 | } |
346 | 346 | ||
347 | /* |
347 | /* |
348 | * Create copy of the address space area. |
348 | * Create copy of the address space area. |
349 | */ |
349 | */ |
350 | if (!as_area_create(as, flags, size, dst_base)) { |
350 | if (!as_area_create(as, flags, size, dst_base)) { |
351 | /* |
351 | /* |
352 | * Destination address space area could not be created. |
352 | * Destination address space area could not be created. |
353 | */ |
353 | */ |
354 | spinlock_unlock(&t->lock); |
354 | spinlock_unlock(&t->lock); |
355 | interrupts_restore(ipl); |
355 | interrupts_restore(ipl); |
356 | return ENOMEM; |
356 | return ENOMEM; |
357 | } |
357 | } |
358 | 358 | ||
359 | /* |
359 | /* |
360 | * NOTE: we have just introduced a race condition. |
360 | * NOTE: we have just introduced a race condition. |
361 | * The destination task can try to attempt the newly |
361 | * The destination task can try to attempt the newly |
362 | * created area before its mapping is copied from |
362 | * created area before its mapping is copied from |
363 | * the source address space area. In result, frames |
363 | * the source address space area. In result, frames |
364 | * can get lost. |
364 | * can get lost. |
365 | * |
365 | * |
366 | * Currently, this race is not solved, but one of the |
366 | * Currently, this race is not solved, but one of the |
367 | * possible solutions would be to sleep in as_page_fault() |
367 | * possible solutions would be to sleep in as_page_fault() |
368 | * when this situation is detected. |
368 | * when this situation is detected. |
369 | */ |
369 | */ |
370 | 370 | ||
371 | memsetb((__address) &t->accept_arg, sizeof(as_area_acptsnd_arg_t), 0); |
371 | memsetb((__address) &t->accept_arg, sizeof(as_area_acptsnd_arg_t), 0); |
372 | spinlock_unlock(&t->lock); |
372 | spinlock_unlock(&t->lock); |
373 | 373 | ||
374 | /* |
374 | /* |
375 | * Avoid deadlock by first locking the address space with lower address. |
375 | * Avoid deadlock by first locking the address space with lower address. |
376 | */ |
376 | */ |
377 | if (as < AS) { |
377 | if (as < AS) { |
378 | spinlock_lock(&as->lock); |
378 | spinlock_lock(&as->lock); |
379 | spinlock_lock(&AS->lock); |
379 | spinlock_lock(&AS->lock); |
380 | } else { |
380 | } else { |
381 | spinlock_lock(&AS->lock); |
381 | spinlock_lock(&AS->lock); |
382 | spinlock_lock(&as->lock); |
382 | spinlock_lock(&as->lock); |
383 | } |
383 | } |
384 | 384 | ||
385 | for (i = 0; i < SIZE2FRAMES(size); i++) { |
385 | for (i = 0; i < SIZE2FRAMES(size); i++) { |
386 | pte_t *pte; |
386 | pte_t *pte; |
387 | __address frame; |
387 | __address frame; |
388 | 388 | ||
389 | page_table_lock(AS, false); |
389 | page_table_lock(AS, false); |
390 | pte = page_mapping_find(AS, base + i*PAGE_SIZE); |
390 | pte = page_mapping_find(AS, base + i*PAGE_SIZE); |
391 | if (pte && PTE_VALID(pte)) { |
391 | if (pte && PTE_VALID(pte)) { |
392 | ASSERT(PTE_PRESENT(pte)); |
392 | ASSERT(PTE_PRESENT(pte)); |
393 | frame = PTE_GET_FRAME(pte); |
393 | frame = PTE_GET_FRAME(pte); |
394 | if (!(flags & AS_AREA_DEVICE)) { |
394 | if (!(flags & AS_AREA_DEVICE)) |
395 | /* TODO: increment frame reference count */ |
395 | frame_reference_add(ADDR2PFN(frame)); |
396 | } |
- | |
397 | page_table_unlock(AS, false); |
396 | page_table_unlock(AS, false); |
398 | } else { |
397 | } else { |
399 | page_table_unlock(AS, false); |
398 | page_table_unlock(AS, false); |
400 | continue; |
399 | continue; |
401 | } |
400 | } |
402 | 401 | ||
403 | page_table_lock(as, false); |
402 | page_table_lock(as, false); |
404 | page_mapping_insert(as, dst_base + i*PAGE_SIZE, frame, area_flags_to_page_flags(flags)); |
403 | page_mapping_insert(as, dst_base + i*PAGE_SIZE, frame, area_flags_to_page_flags(flags)); |
405 | page_table_unlock(as, false); |
404 | page_table_unlock(as, false); |
406 | } |
405 | } |
407 | 406 | ||
408 | spinlock_unlock(&AS->lock); |
407 | spinlock_unlock(&AS->lock); |
409 | spinlock_unlock(&as->lock); |
408 | spinlock_unlock(&as->lock); |
410 | interrupts_restore(ipl); |
409 | interrupts_restore(ipl); |
411 | 410 | ||
412 | return 0; |
411 | return 0; |
413 | } |
412 | } |
414 | 413 | ||
415 | /** Initialize mapping for one page of address space. |
414 | /** Initialize mapping for one page of address space. |
416 | * |
415 | * |
417 | * This functions maps 'page' to 'frame' according |
416 | * This functions maps 'page' to 'frame' according |
418 | * to attributes of the address space area to |
417 | * to attributes of the address space area to |
419 | * wich 'page' belongs. |
418 | * wich 'page' belongs. |
420 | * |
419 | * |
421 | * @param as Target address space. |
420 | * @param as Target address space. |
422 | * @param page Virtual page within the area. |
421 | * @param page Virtual page within the area. |
423 | * @param frame Physical frame to which page will be mapped. |
422 | * @param frame Physical frame to which page will be mapped. |
424 | */ |
423 | */ |
425 | void as_set_mapping(as_t *as, __address page, __address frame) |
424 | void as_set_mapping(as_t *as, __address page, __address frame) |
426 | { |
425 | { |
427 | as_area_t *area; |
426 | as_area_t *area; |
428 | ipl_t ipl; |
427 | ipl_t ipl; |
429 | 428 | ||
430 | ipl = interrupts_disable(); |
429 | ipl = interrupts_disable(); |
431 | page_table_lock(as, true); |
430 | page_table_lock(as, true); |
432 | 431 | ||
433 | area = find_area_and_lock(as, page); |
432 | area = find_area_and_lock(as, page); |
434 | if (!area) { |
433 | if (!area) { |
435 | panic("page not part of any as_area\n"); |
434 | panic("page not part of any as_area\n"); |
436 | } |
435 | } |
437 | 436 | ||
438 | page_mapping_insert(as, page, frame, get_area_flags(area)); |
437 | page_mapping_insert(as, page, frame, get_area_flags(area)); |
439 | 438 | ||
440 | spinlock_unlock(&area->lock); |
439 | spinlock_unlock(&area->lock); |
441 | page_table_unlock(as, true); |
440 | page_table_unlock(as, true); |
442 | interrupts_restore(ipl); |
441 | interrupts_restore(ipl); |
443 | } |
442 | } |
444 | 443 | ||
445 | /** Handle page fault within the current address space. |
444 | /** Handle page fault within the current address space. |
446 | * |
445 | * |
447 | * This is the high-level page fault handler. |
446 | * This is the high-level page fault handler. |
448 | * Interrupts are assumed disabled. |
447 | * Interrupts are assumed disabled. |
449 | * |
448 | * |
450 | * @param page Faulting page. |
449 | * @param page Faulting page. |
451 | * |
450 | * |
452 | * @return 0 on page fault, 1 on success. |
451 | * @return 0 on page fault, 1 on success. |
453 | */ |
452 | */ |
454 | int as_page_fault(__address page) |
453 | int as_page_fault(__address page) |
455 | { |
454 | { |
456 | pte_t *pte; |
455 | pte_t *pte; |
457 | as_area_t *area; |
456 | as_area_t *area; |
458 | __address frame; |
457 | __address frame; |
459 | 458 | ||
460 | ASSERT(AS); |
459 | ASSERT(AS); |
461 | 460 | ||
462 | spinlock_lock(&AS->lock); |
461 | spinlock_lock(&AS->lock); |
463 | area = find_area_and_lock(AS, page); |
462 | area = find_area_and_lock(AS, page); |
464 | if (!area) { |
463 | if (!area) { |
465 | /* |
464 | /* |
466 | * No area contained mapping for 'page'. |
465 | * No area contained mapping for 'page'. |
467 | * Signal page fault to low-level handler. |
466 | * Signal page fault to low-level handler. |
468 | */ |
467 | */ |
469 | spinlock_unlock(&AS->lock); |
468 | spinlock_unlock(&AS->lock); |
470 | return 0; |
469 | return 0; |
471 | } |
470 | } |
472 | 471 | ||
473 | ASSERT(!(area->flags & AS_AREA_DEVICE)); |
472 | ASSERT(!(area->flags & AS_AREA_DEVICE)); |
474 | 473 | ||
475 | page_table_lock(AS, false); |
474 | page_table_lock(AS, false); |
476 | 475 | ||
477 | /* |
476 | /* |
478 | * To avoid race condition between two page faults |
477 | * To avoid race condition between two page faults |
479 | * on the same address, we need to make sure |
478 | * on the same address, we need to make sure |
480 | * the mapping has not been already inserted. |
479 | * the mapping has not been already inserted. |
481 | */ |
480 | */ |
482 | if ((pte = page_mapping_find(AS, page))) { |
481 | if ((pte = page_mapping_find(AS, page))) { |
483 | if (PTE_PRESENT(pte)) { |
482 | if (PTE_PRESENT(pte)) { |
484 | page_table_unlock(AS, false); |
483 | page_table_unlock(AS, false); |
485 | spinlock_unlock(&area->lock); |
484 | spinlock_unlock(&area->lock); |
486 | spinlock_unlock(&AS->lock); |
485 | spinlock_unlock(&AS->lock); |
487 | return 1; |
486 | return 1; |
488 | } |
487 | } |
489 | } |
488 | } |
490 | 489 | ||
491 | /* |
490 | /* |
492 | * In general, there can be several reasons that |
491 | * In general, there can be several reasons that |
493 | * can have caused this fault. |
492 | * can have caused this fault. |
494 | * |
493 | * |
495 | * - non-existent mapping: the area is a scratch |
494 | * - non-existent mapping: the area is a scratch |
496 | * area (e.g. stack) and so far has not been |
495 | * area (e.g. stack) and so far has not been |
497 | * allocated a frame for the faulting page |
496 | * allocated a frame for the faulting page |
498 | * |
497 | * |
499 | * - non-present mapping: another possibility, |
498 | * - non-present mapping: another possibility, |
500 | * currently not implemented, would be frame |
499 | * currently not implemented, would be frame |
501 | * reuse; when this becomes a possibility, |
500 | * reuse; when this becomes a possibility, |
502 | * do not forget to distinguish between |
501 | * do not forget to distinguish between |
503 | * the different causes |
502 | * the different causes |
504 | */ |
503 | */ |
505 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
504 | frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0)); |
506 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
505 | memsetb(PA2KA(frame), FRAME_SIZE, 0); |
507 | 506 | ||
508 | /* |
507 | /* |
509 | * Map 'page' to 'frame'. |
508 | * Map 'page' to 'frame'. |
510 | * Note that TLB shootdown is not attempted as only new information is being |
509 | * Note that TLB shootdown is not attempted as only new information is being |
511 | * inserted into page tables. |
510 | * inserted into page tables. |
512 | */ |
511 | */ |
513 | page_mapping_insert(AS, page, frame, get_area_flags(area)); |
512 | page_mapping_insert(AS, page, frame, get_area_flags(area)); |
514 | page_table_unlock(AS, false); |
513 | page_table_unlock(AS, false); |
515 | 514 | ||
516 | spinlock_unlock(&area->lock); |
515 | spinlock_unlock(&area->lock); |
517 | spinlock_unlock(&AS->lock); |
516 | spinlock_unlock(&AS->lock); |
518 | return 1; |
517 | return 1; |
519 | } |
518 | } |
520 | 519 | ||
521 | /** Switch address spaces. |
520 | /** Switch address spaces. |
522 | * |
521 | * |
523 | * @param old Old address space or NULL. |
522 | * @param old Old address space or NULL. |
524 | * @param new New address space. |
523 | * @param new New address space. |
525 | */ |
524 | */ |
526 | void as_switch(as_t *old, as_t *new) |
525 | void as_switch(as_t *old, as_t *new) |
527 | { |
526 | { |
528 | ipl_t ipl; |
527 | ipl_t ipl; |
529 | bool needs_asid = false; |
528 | bool needs_asid = false; |
530 | 529 | ||
531 | ipl = interrupts_disable(); |
530 | ipl = interrupts_disable(); |
532 | spinlock_lock(&as_lock); |
531 | spinlock_lock(&as_lock); |
533 | 532 | ||
534 | /* |
533 | /* |
535 | * First, take care of the old address space. |
534 | * First, take care of the old address space. |
536 | */ |
535 | */ |
537 | if (old) { |
536 | if (old) { |
538 | spinlock_lock(&old->lock); |
537 | spinlock_lock(&old->lock); |
539 | ASSERT(old->refcount); |
538 | ASSERT(old->refcount); |
540 | if((--old->refcount == 0) && (old != AS_KERNEL)) { |
539 | if((--old->refcount == 0) && (old != AS_KERNEL)) { |
541 | /* |
540 | /* |
542 | * The old address space is no longer active on |
541 | * The old address space is no longer active on |
543 | * any processor. It can be appended to the |
542 | * any processor. It can be appended to the |
544 | * list of inactive address spaces with assigned |
543 | * list of inactive address spaces with assigned |
545 | * ASID. |
544 | * ASID. |
546 | */ |
545 | */ |
547 | ASSERT(old->asid != ASID_INVALID); |
546 | ASSERT(old->asid != ASID_INVALID); |
548 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head); |
547 | list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head); |
549 | } |
548 | } |
550 | spinlock_unlock(&old->lock); |
549 | spinlock_unlock(&old->lock); |
551 | } |
550 | } |
552 | 551 | ||
553 | /* |
552 | /* |
554 | * Second, prepare the new address space. |
553 | * Second, prepare the new address space. |
555 | */ |
554 | */ |
556 | spinlock_lock(&new->lock); |
555 | spinlock_lock(&new->lock); |
557 | if ((new->refcount++ == 0) && (new != AS_KERNEL)) { |
556 | if ((new->refcount++ == 0) && (new != AS_KERNEL)) { |
558 | if (new->asid != ASID_INVALID) |
557 | if (new->asid != ASID_INVALID) |
559 | list_remove(&new->inactive_as_with_asid_link); |
558 | list_remove(&new->inactive_as_with_asid_link); |
560 | else |
559 | else |
561 | needs_asid = true; /* defer call to asid_get() until new->lock is released */ |
560 | needs_asid = true; /* defer call to asid_get() until new->lock is released */ |
562 | } |
561 | } |
563 | SET_PTL0_ADDRESS(new->page_table); |
562 | SET_PTL0_ADDRESS(new->page_table); |
564 | spinlock_unlock(&new->lock); |
563 | spinlock_unlock(&new->lock); |
565 | 564 | ||
566 | if (needs_asid) { |
565 | if (needs_asid) { |
567 | /* |
566 | /* |
568 | * Allocation of new ASID was deferred |
567 | * Allocation of new ASID was deferred |
569 | * until now in order to avoid deadlock. |
568 | * until now in order to avoid deadlock. |
570 | */ |
569 | */ |
571 | asid_t asid; |
570 | asid_t asid; |
572 | 571 | ||
573 | asid = asid_get(); |
572 | asid = asid_get(); |
574 | spinlock_lock(&new->lock); |
573 | spinlock_lock(&new->lock); |
575 | new->asid = asid; |
574 | new->asid = asid; |
576 | spinlock_unlock(&new->lock); |
575 | spinlock_unlock(&new->lock); |
577 | } |
576 | } |
578 | spinlock_unlock(&as_lock); |
577 | spinlock_unlock(&as_lock); |
579 | interrupts_restore(ipl); |
578 | interrupts_restore(ipl); |
580 | 579 | ||
581 | /* |
580 | /* |
582 | * Perform architecture-specific steps. |
581 | * Perform architecture-specific steps. |
583 | * (e.g. write ASID to hardware register etc.) |
582 | * (e.g. write ASID to hardware register etc.) |
584 | */ |
583 | */ |
585 | as_install_arch(new); |
584 | as_install_arch(new); |
586 | 585 | ||
587 | AS = new; |
586 | AS = new; |
588 | } |
587 | } |
589 | 588 | ||
590 | /** Convert address space area flags to page flags. |
589 | /** Convert address space area flags to page flags. |
591 | * |
590 | * |
592 | * @param aflags Flags of some address space area. |
591 | * @param aflags Flags of some address space area. |
593 | * |
592 | * |
594 | * @return Flags to be passed to page_mapping_insert(). |
593 | * @return Flags to be passed to page_mapping_insert(). |
595 | */ |
594 | */ |
596 | int area_flags_to_page_flags(int aflags) |
595 | int area_flags_to_page_flags(int aflags) |
597 | { |
596 | { |
598 | int flags; |
597 | int flags; |
599 | 598 | ||
600 | flags = PAGE_USER | PAGE_PRESENT; |
599 | flags = PAGE_USER | PAGE_PRESENT; |
601 | 600 | ||
602 | if (aflags & AS_AREA_READ) |
601 | if (aflags & AS_AREA_READ) |
603 | flags |= PAGE_READ; |
602 | flags |= PAGE_READ; |
604 | 603 | ||
605 | if (aflags & AS_AREA_WRITE) |
604 | if (aflags & AS_AREA_WRITE) |
606 | flags |= PAGE_WRITE; |
605 | flags |= PAGE_WRITE; |
607 | 606 | ||
608 | if (aflags & AS_AREA_EXEC) |
607 | if (aflags & AS_AREA_EXEC) |
609 | flags |= PAGE_EXEC; |
608 | flags |= PAGE_EXEC; |
610 | 609 | ||
611 | if (!(aflags & AS_AREA_DEVICE)) |
610 | if (!(aflags & AS_AREA_DEVICE)) |
612 | flags |= PAGE_CACHEABLE; |
611 | flags |= PAGE_CACHEABLE; |
613 | 612 | ||
614 | return flags; |
613 | return flags; |
615 | } |
614 | } |
616 | 615 | ||
617 | /** Compute flags for virtual address translation subsytem. |
616 | /** Compute flags for virtual address translation subsytem. |
618 | * |
617 | * |
619 | * The address space area must be locked. |
618 | * The address space area must be locked. |
620 | * Interrupts must be disabled. |
619 | * Interrupts must be disabled. |
621 | * |
620 | * |
622 | * @param a Address space area. |
621 | * @param a Address space area. |
623 | * |
622 | * |
624 | * @return Flags to be used in page_mapping_insert(). |
623 | * @return Flags to be used in page_mapping_insert(). |
625 | */ |
624 | */ |
626 | int get_area_flags(as_area_t *a) |
625 | int get_area_flags(as_area_t *a) |
627 | { |
626 | { |
628 | return area_flags_to_page_flags(a->flags); |
627 | return area_flags_to_page_flags(a->flags); |
629 | } |
628 | } |
630 | 629 | ||
631 | /** Create page table. |
630 | /** Create page table. |
632 | * |
631 | * |
633 | * Depending on architecture, create either address space |
632 | * Depending on architecture, create either address space |
634 | * private or global page table. |
633 | * private or global page table. |
635 | * |
634 | * |
636 | * @param flags Flags saying whether the page table is for kernel address space. |
635 | * @param flags Flags saying whether the page table is for kernel address space. |
637 | * |
636 | * |
638 | * @return First entry of the page table. |
637 | * @return First entry of the page table. |
639 | */ |
638 | */ |
640 | pte_t *page_table_create(int flags) |
639 | pte_t *page_table_create(int flags) |
641 | { |
640 | { |
642 | ASSERT(as_operations); |
641 | ASSERT(as_operations); |
643 | ASSERT(as_operations->page_table_create); |
642 | ASSERT(as_operations->page_table_create); |
644 | 643 | ||
645 | return as_operations->page_table_create(flags); |
644 | return as_operations->page_table_create(flags); |
646 | } |
645 | } |
647 | 646 | ||
648 | /** Lock page table. |
647 | /** Lock page table. |
649 | * |
648 | * |
650 | * This function should be called before any page_mapping_insert(), |
649 | * This function should be called before any page_mapping_insert(), |
651 | * page_mapping_remove() and page_mapping_find(). |
650 | * page_mapping_remove() and page_mapping_find(). |
652 | * |
651 | * |
653 | * Locking order is such that address space areas must be locked |
652 | * Locking order is such that address space areas must be locked |
654 | * prior to this call. Address space can be locked prior to this |
653 | * prior to this call. Address space can be locked prior to this |
655 | * call in which case the lock argument is false. |
654 | * call in which case the lock argument is false. |
656 | * |
655 | * |
657 | * @param as Address space. |
656 | * @param as Address space. |
658 | * @param as_locked If false, do not attempt to lock as->lock. |
657 | * @param as_locked If false, do not attempt to lock as->lock. |
659 | */ |
658 | */ |
660 | void page_table_lock(as_t *as, bool lock) |
659 | void page_table_lock(as_t *as, bool lock) |
661 | { |
660 | { |
662 | ASSERT(as_operations); |
661 | ASSERT(as_operations); |
663 | ASSERT(as_operations->page_table_lock); |
662 | ASSERT(as_operations->page_table_lock); |
664 | 663 | ||
665 | as_operations->page_table_lock(as, lock); |
664 | as_operations->page_table_lock(as, lock); |
666 | } |
665 | } |
667 | 666 | ||
668 | /** Unlock page table. |
667 | /** Unlock page table. |
669 | * |
668 | * |
670 | * @param as Address space. |
669 | * @param as Address space. |
671 | * @param as_locked If false, do not attempt to unlock as->lock. |
670 | * @param as_locked If false, do not attempt to unlock as->lock. |
672 | */ |
671 | */ |
673 | void page_table_unlock(as_t *as, bool unlock) |
672 | void page_table_unlock(as_t *as, bool unlock) |
674 | { |
673 | { |
675 | ASSERT(as_operations); |
674 | ASSERT(as_operations); |
676 | ASSERT(as_operations->page_table_unlock); |
675 | ASSERT(as_operations->page_table_unlock); |
677 | 676 | ||
678 | as_operations->page_table_unlock(as, unlock); |
677 | as_operations->page_table_unlock(as, unlock); |
679 | } |
678 | } |
680 | 679 | ||
681 | 680 | ||
682 | /** Find address space area and lock it. |
681 | /** Find address space area and lock it. |
683 | * |
682 | * |
684 | * The address space must be locked and interrupts must be disabled. |
683 | * The address space must be locked and interrupts must be disabled. |
685 | * |
684 | * |
686 | * @param as Address space. |
685 | * @param as Address space. |
687 | * @param va Virtual address. |
686 | * @param va Virtual address. |
688 | * |
687 | * |
689 | * @return Locked address space area containing va on success or NULL on failure. |
688 | * @return Locked address space area containing va on success or NULL on failure. |
690 | */ |
689 | */ |
691 | as_area_t *find_area_and_lock(as_t *as, __address va) |
690 | as_area_t *find_area_and_lock(as_t *as, __address va) |
692 | { |
691 | { |
693 | as_area_t *a; |
692 | as_area_t *a; |
694 | btree_node_t *leaf, *lnode; |
693 | btree_node_t *leaf, *lnode; |
695 | int i; |
694 | int i; |
696 | 695 | ||
697 | a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf); |
696 | a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf); |
698 | if (a) { |
697 | if (a) { |
699 | /* va is the base address of an address space area */ |
698 | /* va is the base address of an address space area */ |
700 | spinlock_lock(&a->lock); |
699 | spinlock_lock(&a->lock); |
701 | return a; |
700 | return a; |
702 | } |
701 | } |
703 | 702 | ||
704 | /* |
703 | /* |
705 | * Search the leaf node and the righmost record of its left neighbour |
704 | * Search the leaf node and the righmost record of its left neighbour |
706 | * to find out whether this is a miss or va belongs to an address |
705 | * to find out whether this is a miss or va belongs to an address |
707 | * space area found there. |
706 | * space area found there. |
708 | */ |
707 | */ |
709 | 708 | ||
710 | /* First, search the leaf node itself. */ |
709 | /* First, search the leaf node itself. */ |
711 | for (i = 0; i < leaf->keys; i++) { |
710 | for (i = 0; i < leaf->keys; i++) { |
712 | a = (as_area_t *) leaf->value[i]; |
711 | a = (as_area_t *) leaf->value[i]; |
713 | spinlock_lock(&a->lock); |
712 | spinlock_lock(&a->lock); |
714 | if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) { |
713 | if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) { |
715 | return a; |
714 | return a; |
716 | } |
715 | } |
717 | spinlock_unlock(&a->lock); |
716 | spinlock_unlock(&a->lock); |
718 | } |
717 | } |
719 | 718 | ||
720 | /* |
719 | /* |
721 | * Second, locate the left neighbour and test its last record. |
720 | * Second, locate the left neighbour and test its last record. |
722 | * Because of its position in the B+tree, it must have base < va. |
721 | * Because of its position in the B+tree, it must have base < va. |
723 | */ |
722 | */ |
724 | if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
723 | if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
725 | a = (as_area_t *) lnode->value[lnode->keys - 1]; |
724 | a = (as_area_t *) lnode->value[lnode->keys - 1]; |
726 | spinlock_lock(&a->lock); |
725 | spinlock_lock(&a->lock); |
727 | if (va < a->base + a->pages * PAGE_SIZE) { |
726 | if (va < a->base + a->pages * PAGE_SIZE) { |
728 | return a; |
727 | return a; |
729 | } |
728 | } |
730 | spinlock_unlock(&a->lock); |
729 | spinlock_unlock(&a->lock); |
731 | } |
730 | } |
732 | 731 | ||
733 | return NULL; |
732 | return NULL; |
734 | } |
733 | } |
735 | 734 | ||
736 | /** Check area conflicts with other areas. |
735 | /** Check area conflicts with other areas. |
737 | * |
736 | * |
738 | * The address space must be locked and interrupts must be disabled. |
737 | * The address space must be locked and interrupts must be disabled. |
739 | * |
738 | * |
740 | * @param as Address space. |
739 | * @param as Address space. |
741 | * @param va Starting virtual address of the area being tested. |
740 | * @param va Starting virtual address of the area being tested. |
742 | * @param size Size of the area being tested. |
741 | * @param size Size of the area being tested. |
743 | * @param avoid_area Do not touch this area. |
742 | * @param avoid_area Do not touch this area. |
744 | * |
743 | * |
745 | * @return True if there is no conflict, false otherwise. |
744 | * @return True if there is no conflict, false otherwise. |
746 | */ |
745 | */ |
747 | bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area) |
746 | bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area) |
748 | { |
747 | { |
749 | as_area_t *a; |
748 | as_area_t *a; |
750 | btree_node_t *leaf, *node; |
749 | btree_node_t *leaf, *node; |
751 | int i; |
750 | int i; |
752 | 751 | ||
753 | /* |
752 | /* |
754 | * We don't want any area to have conflicts with NULL page. |
753 | * We don't want any area to have conflicts with NULL page. |
755 | */ |
754 | */ |
756 | if (overlaps(va, size, NULL, PAGE_SIZE)) |
755 | if (overlaps(va, size, NULL, PAGE_SIZE)) |
757 | return false; |
756 | return false; |
758 | 757 | ||
759 | /* |
758 | /* |
760 | * The leaf node is found in O(log n), where n is proportional to |
759 | * The leaf node is found in O(log n), where n is proportional to |
761 | * the number of address space areas belonging to as. |
760 | * the number of address space areas belonging to as. |
762 | * The check for conflicts is then attempted on the rightmost |
761 | * The check for conflicts is then attempted on the rightmost |
763 | * record in the left neighbour, the leftmost record in the right |
762 | * record in the left neighbour, the leftmost record in the right |
764 | * neighbour and all records in the leaf node itself. |
763 | * neighbour and all records in the leaf node itself. |
765 | */ |
764 | */ |
766 | 765 | ||
767 | if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) { |
766 | if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) { |
768 | if (a != avoid_area) |
767 | if (a != avoid_area) |
769 | return false; |
768 | return false; |
770 | } |
769 | } |
771 | 770 | ||
772 | /* First, check the two border cases. */ |
771 | /* First, check the two border cases. */ |
773 | if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
772 | if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) { |
774 | a = (as_area_t *) node->value[node->keys - 1]; |
773 | a = (as_area_t *) node->value[node->keys - 1]; |
775 | spinlock_lock(&a->lock); |
774 | spinlock_lock(&a->lock); |
776 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
775 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
777 | spinlock_unlock(&a->lock); |
776 | spinlock_unlock(&a->lock); |
778 | return false; |
777 | return false; |
779 | } |
778 | } |
780 | spinlock_unlock(&a->lock); |
779 | spinlock_unlock(&a->lock); |
781 | } |
780 | } |
782 | if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) { |
781 | if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) { |
783 | a = (as_area_t *) node->value[0]; |
782 | a = (as_area_t *) node->value[0]; |
784 | spinlock_lock(&a->lock); |
783 | spinlock_lock(&a->lock); |
785 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
784 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
786 | spinlock_unlock(&a->lock); |
785 | spinlock_unlock(&a->lock); |
787 | return false; |
786 | return false; |
788 | } |
787 | } |
789 | spinlock_unlock(&a->lock); |
788 | spinlock_unlock(&a->lock); |
790 | } |
789 | } |
791 | 790 | ||
792 | /* Second, check the leaf node. */ |
791 | /* Second, check the leaf node. */ |
793 | for (i = 0; i < leaf->keys; i++) { |
792 | for (i = 0; i < leaf->keys; i++) { |
794 | a = (as_area_t *) leaf->value[i]; |
793 | a = (as_area_t *) leaf->value[i]; |
795 | 794 | ||
796 | if (a == avoid_area) |
795 | if (a == avoid_area) |
797 | continue; |
796 | continue; |
798 | 797 | ||
799 | spinlock_lock(&a->lock); |
798 | spinlock_lock(&a->lock); |
800 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
799 | if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) { |
801 | spinlock_unlock(&a->lock); |
800 | spinlock_unlock(&a->lock); |
802 | return false; |
801 | return false; |
803 | } |
802 | } |
804 | spinlock_unlock(&a->lock); |
803 | spinlock_unlock(&a->lock); |
805 | } |
804 | } |
806 | 805 | ||
807 | /* |
806 | /* |
808 | * So far, the area does not conflict with other areas. |
807 | * So far, the area does not conflict with other areas. |
809 | * Check if it doesn't conflict with kernel address space. |
808 | * Check if it doesn't conflict with kernel address space. |
810 | */ |
809 | */ |
811 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) { |
810 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) { |
812 | return !overlaps(va, size, |
811 | return !overlaps(va, size, |
813 | KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START); |
812 | KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START); |
814 | } |
813 | } |
815 | 814 | ||
816 | return true; |
815 | return true; |
817 | } |
816 | } |
818 | 817 | ||
819 | /* |
818 | /* |
820 | * Address space related syscalls. |
819 | * Address space related syscalls. |
821 | */ |
820 | */ |
822 | 821 | ||
823 | /** Wrapper for as_area_create(). */ |
822 | /** Wrapper for as_area_create(). */ |
824 | __native sys_as_area_create(__address address, size_t size, int flags) |
823 | __native sys_as_area_create(__address address, size_t size, int flags) |
825 | { |
824 | { |
826 | if (as_area_create(AS, flags, size, address)) |
825 | if (as_area_create(AS, flags, size, address)) |
827 | return (__native) address; |
826 | return (__native) address; |
828 | else |
827 | else |
829 | return (__native) -1; |
828 | return (__native) -1; |
830 | } |
829 | } |
831 | 830 | ||
832 | /** Wrapper for as_area_resize. */ |
831 | /** Wrapper for as_area_resize. */ |
833 | __native sys_as_area_resize(__address address, size_t size, int flags) |
832 | __native sys_as_area_resize(__address address, size_t size, int flags) |
834 | { |
833 | { |
835 | return as_area_resize(AS, address, size, 0); |
834 | return as_area_resize(AS, address, size, 0); |
836 | } |
835 | } |
837 | 836 | ||
838 | /** Prepare task for accepting address space area from another task. |
837 | /** Prepare task for accepting address space area from another task. |
839 | * |
838 | * |
840 | * @param uspace_accept_arg Accept structure passed from userspace. |
839 | * @param uspace_accept_arg Accept structure passed from userspace. |
841 | * |
840 | * |
842 | * @return EPERM if the task ID encapsulated in @uspace_accept_arg references |
841 | * @return EPERM if the task ID encapsulated in @uspace_accept_arg references |
843 | * TASK. Otherwise zero is returned. |
842 | * TASK. Otherwise zero is returned. |
844 | */ |
843 | */ |
845 | __native sys_as_area_accept(as_area_acptsnd_arg_t *uspace_accept_arg) |
844 | __native sys_as_area_accept(as_area_acptsnd_arg_t *uspace_accept_arg) |
846 | { |
845 | { |
847 | as_area_acptsnd_arg_t arg; |
846 | as_area_acptsnd_arg_t arg; |
848 | 847 | ||
849 | copy_from_uspace(&arg, uspace_accept_arg, sizeof(as_area_acptsnd_arg_t)); |
848 | copy_from_uspace(&arg, uspace_accept_arg, sizeof(as_area_acptsnd_arg_t)); |
850 | 849 | ||
851 | if (!arg.size) |
850 | if (!arg.size) |
852 | return (__native) EPERM; |
851 | return (__native) EPERM; |
853 | 852 | ||
854 | if (arg.task_id == TASK->taskid) { |
853 | if (arg.task_id == TASK->taskid) { |
855 | /* |
854 | /* |
856 | * Accepting from itself is not allowed. |
855 | * Accepting from itself is not allowed. |
857 | */ |
856 | */ |
858 | return (__native) EPERM; |
857 | return (__native) EPERM; |
859 | } |
858 | } |
860 | 859 | ||
861 | memcpy(&TASK->accept_arg, &arg, sizeof(as_area_acptsnd_arg_t)); |
860 | memcpy(&TASK->accept_arg, &arg, sizeof(as_area_acptsnd_arg_t)); |
862 | 861 | ||
863 | return 0; |
862 | return 0; |
864 | } |
863 | } |
865 | 864 | ||
866 | /** Wrapper for as_area_send. */ |
865 | /** Wrapper for as_area_send. */ |
867 | __native sys_as_area_send(as_area_acptsnd_arg_t *uspace_send_arg) |
866 | __native sys_as_area_send(as_area_acptsnd_arg_t *uspace_send_arg) |
868 | { |
867 | { |
869 | as_area_acptsnd_arg_t arg; |
868 | as_area_acptsnd_arg_t arg; |
870 | 869 | ||
871 | copy_from_uspace(&arg, uspace_send_arg, sizeof(as_area_acptsnd_arg_t)); |
870 | copy_from_uspace(&arg, uspace_send_arg, sizeof(as_area_acptsnd_arg_t)); |
872 | 871 | ||
873 | if (!arg.size) |
872 | if (!arg.size) |
874 | return (__native) EPERM; |
873 | return (__native) EPERM; |
875 | 874 | ||
876 | if (arg.task_id == TASK->taskid) { |
875 | if (arg.task_id == TASK->taskid) { |
877 | /* |
876 | /* |
878 | * Sending to itself is not allowed. |
877 | * Sending to itself is not allowed. |
879 | */ |
878 | */ |
880 | return (__native) EPERM; |
879 | return (__native) EPERM; |
881 | } |
880 | } |
882 | 881 | ||
883 | return (__native) as_area_send(arg.task_id, (__address) arg.base, arg.size, arg.flags); |
882 | return (__native) as_area_send(arg.task_id, (__address) arg.base, arg.size, arg.flags); |
884 | } |
883 | } |
885 | 884 |