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