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