Rev 257 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 257 | Rev 391 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 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 | #include <mm/vm.h> |
29 | #include <mm/vm.h> |
30 | #include <mm/page.h> |
30 | #include <mm/page.h> |
31 | #include <mm/frame.h> |
31 | #include <mm/frame.h> |
32 | #include <mm/tlb.h> |
32 | #include <mm/tlb.h> |
33 | #include <mm/heap.h> |
33 | #include <mm/heap.h> |
34 | #include <arch/mm/page.h> |
34 | #include <arch/mm/page.h> |
- | 35 | #include <arch/mm/asid.h> |
|
- | 36 | #include <arch/mm/vm.h> |
|
35 | #include <arch/types.h> |
37 | #include <arch/types.h> |
36 | #include <typedefs.h> |
38 | #include <typedefs.h> |
37 | #include <synch/spinlock.h> |
39 | #include <synch/spinlock.h> |
38 | #include <config.h> |
40 | #include <config.h> |
39 | #include <list.h> |
41 | #include <list.h> |
40 | #include <panic.h> |
42 | #include <panic.h> |
41 | #include <arch/asm.h> |
43 | #include <arch/asm.h> |
42 | #include <debug.h> |
44 | #include <debug.h> |
43 | #include <memstr.h> |
45 | #include <memstr.h> |
44 | #include <arch.h> |
46 | #include <arch.h> |
45 | 47 | ||
46 | #define KAS_START_INDEX PTL0_INDEX(KERNEL_ADDRESS_SPACE_START) |
48 | #define KAS_START_INDEX PTL0_INDEX(KERNEL_ADDRESS_SPACE_START) |
47 | #define KAS_END_INDEX PTL0_INDEX(KERNEL_ADDRESS_SPACE_END) |
49 | #define KAS_END_INDEX PTL0_INDEX(KERNEL_ADDRESS_SPACE_END) |
48 | #define KAS_INDICES (1+(KAS_END_INDEX-KAS_START_INDEX)) |
50 | #define KAS_INDICES (1+(KAS_END_INDEX-KAS_START_INDEX)) |
49 | 51 | ||
50 | vm_t *vm_create(pte_t *ptl0) |
52 | vm_t *vm_create(pte_t *ptl0) |
51 | { |
53 | { |
52 | vm_t *m; |
54 | vm_t *m; |
53 | 55 | ||
54 | m = (vm_t *) malloc(sizeof(vm_t)); |
56 | m = (vm_t *) malloc(sizeof(vm_t)); |
55 | if (m) { |
57 | if (m) { |
56 | spinlock_initialize(&m->lock); |
58 | spinlock_initialize(&m->lock); |
57 | list_initialize(&m->vm_area_head); |
59 | list_initialize(&m->vm_area_head); |
58 | 60 | ||
- | 61 | m->asid = asid_get(); |
|
- | 62 | ||
59 | /* |
63 | /* |
60 | * Each vm_t is supposed to have its own page table. |
64 | * Each vm_t is supposed to have its own page table. |
61 | * It is either passed one or it has to allocate and set one up. |
65 | * It is either passed one or it has to allocate and set one up. |
62 | */ |
66 | */ |
63 | m->ptl0 = ptl0; |
67 | m->ptl0 = ptl0; |
64 | if (!m->ptl0) { |
68 | if (!m->ptl0) { |
65 | pte_t *src_ptl0, *dst_ptl0; |
69 | pte_t *src_ptl0, *dst_ptl0; |
66 | 70 | ||
67 | src_ptl0 = (pte_t *) PA2KA((__address) GET_PTL0_ADDRESS()); |
71 | src_ptl0 = (pte_t *) PA2KA((__address) GET_PTL0_ADDRESS()); |
68 | dst_ptl0 = (pte_t *) frame_alloc(FRAME_KA | FRAME_PANIC); |
72 | dst_ptl0 = (pte_t *) frame_alloc(FRAME_KA | FRAME_PANIC); |
69 | 73 | ||
70 | // memsetb((__address) dst_ptl0, PAGE_SIZE, 0); |
74 | // memsetb((__address) dst_ptl0, PAGE_SIZE, 0); |
71 | // memcpy((void *) &dst_ptl0[KAS_START_INDEX], (void *) &src_ptl0[KAS_START_INDEX], KAS_INDICES); |
75 | // memcpy((void *) &dst_ptl0[KAS_START_INDEX], (void *) &src_ptl0[KAS_START_INDEX], KAS_INDICES); |
72 | 76 | ||
73 | memcpy((void *) dst_ptl0,(void *) src_ptl0, PAGE_SIZE); |
77 | memcpy((void *) dst_ptl0,(void *) src_ptl0, PAGE_SIZE); |
74 | 78 | ||
75 | m->ptl0 = (pte_t *) KA2PA((__address) dst_ptl0); |
79 | m->ptl0 = (pte_t *) KA2PA((__address) dst_ptl0); |
76 | } |
80 | } |
77 | } |
81 | } |
78 | 82 | ||
79 | return m; |
83 | return m; |
80 | } |
84 | } |
81 | 85 | ||
82 | void vm_destroy(vm_t *m) |
86 | void vm_destroy(vm_t *m) |
83 | { |
87 | { |
84 | } |
88 | } |
85 | 89 | ||
86 | vm_area_t *vm_area_create(vm_t *m, vm_type_t type, size_t size, __address addr) |
90 | vm_area_t *vm_area_create(vm_t *m, vm_type_t type, size_t size, __address addr) |
87 | { |
91 | { |
88 | pri_t pri; |
92 | pri_t pri; |
89 | vm_area_t *a; |
93 | vm_area_t *a; |
90 | 94 | ||
91 | if (addr % PAGE_SIZE) |
95 | if (addr % PAGE_SIZE) |
92 | panic("addr not aligned to a page boundary"); |
96 | panic("addr not aligned to a page boundary"); |
93 | 97 | ||
94 | pri = cpu_priority_high(); |
98 | pri = cpu_priority_high(); |
95 | spinlock_lock(&m->lock); |
99 | spinlock_lock(&m->lock); |
96 | 100 | ||
97 | /* |
101 | /* |
98 | * TODO: test vm_area which is to be created doesn't overlap with an existing one. |
102 | * TODO: test vm_area which is to be created doesn't overlap with an existing one. |
99 | */ |
103 | */ |
100 | 104 | ||
101 | a = (vm_area_t *) malloc(sizeof(vm_area_t)); |
105 | a = (vm_area_t *) malloc(sizeof(vm_area_t)); |
102 | if (a) { |
106 | if (a) { |
103 | int i; |
107 | int i; |
104 | 108 | ||
105 | a->mapping = (__address *) malloc(size * sizeof(__address)); |
109 | a->mapping = (__address *) malloc(size * sizeof(__address)); |
106 | if (!a->mapping) { |
110 | if (!a->mapping) { |
107 | free(a); |
111 | free(a); |
108 | spinlock_unlock(&m->lock); |
112 | spinlock_unlock(&m->lock); |
109 | cpu_priority_restore(pri); |
113 | cpu_priority_restore(pri); |
110 | return NULL; |
114 | return NULL; |
111 | } |
115 | } |
112 | 116 | ||
113 | for (i=0; i<size; i++) |
117 | for (i=0; i<size; i++) |
114 | a->mapping[i] = frame_alloc(0); |
118 | a->mapping[i] = frame_alloc(0); |
115 | 119 | ||
116 | spinlock_initialize(&a->lock); |
120 | spinlock_initialize(&a->lock); |
117 | 121 | ||
118 | link_initialize(&a->link); |
122 | link_initialize(&a->link); |
119 | a->type = type; |
123 | a->type = type; |
120 | a->size = size; |
124 | a->size = size; |
121 | a->address = addr; |
125 | a->address = addr; |
122 | 126 | ||
123 | list_append(&a->link, &m->vm_area_head); |
127 | list_append(&a->link, &m->vm_area_head); |
124 | 128 | ||
125 | } |
129 | } |
126 | 130 | ||
127 | spinlock_unlock(&m->lock); |
131 | spinlock_unlock(&m->lock); |
128 | cpu_priority_restore(pri); |
132 | cpu_priority_restore(pri); |
129 | 133 | ||
130 | return a; |
134 | return a; |
131 | } |
135 | } |
132 | 136 | ||
133 | void vm_area_destroy(vm_area_t *a) |
137 | void vm_area_destroy(vm_area_t *a) |
134 | { |
138 | { |
135 | } |
139 | } |
136 | 140 | ||
137 | void vm_area_map(vm_area_t *a, vm_t *m) |
141 | void vm_area_map(vm_area_t *a, vm_t *m) |
138 | { |
142 | { |
139 | int i, flags; |
143 | int i, flags; |
140 | pri_t pri; |
144 | pri_t pri; |
141 | 145 | ||
142 | pri = cpu_priority_high(); |
146 | pri = cpu_priority_high(); |
143 | spinlock_lock(&m->lock); |
147 | spinlock_lock(&m->lock); |
144 | spinlock_lock(&a->lock); |
148 | spinlock_lock(&a->lock); |
145 | 149 | ||
146 | switch (a->type) { |
150 | switch (a->type) { |
147 | case VMA_TEXT: |
151 | case VMA_TEXT: |
148 | flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
152 | flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
149 | break; |
153 | break; |
150 | case VMA_DATA: |
154 | case VMA_DATA: |
151 | case VMA_STACK: |
155 | case VMA_STACK: |
152 | flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
156 | flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
153 | break; |
157 | break; |
154 | default: |
158 | default: |
155 | panic("unexpected vm_type_t %d", a->type); |
159 | panic("unexpected vm_type_t %d", a->type); |
156 | } |
160 | } |
157 | 161 | ||
158 | ASSERT(m->ptl0); |
162 | ASSERT(m->ptl0); |
159 | for (i=0; i<a->size; i++) |
163 | for (i=0; i<a->size; i++) |
160 | map_page_to_frame(a->address + i*PAGE_SIZE, a->mapping[i], flags, (__address) m->ptl0); |
164 | map_page_to_frame(a->address + i*PAGE_SIZE, a->mapping[i], flags, (__address) m->ptl0); |
161 | 165 | ||
162 | spinlock_unlock(&a->lock); |
166 | spinlock_unlock(&a->lock); |
163 | spinlock_unlock(&m->lock); |
167 | spinlock_unlock(&m->lock); |
164 | cpu_priority_restore(pri); |
168 | cpu_priority_restore(pri); |
165 | } |
169 | } |
166 | 170 | ||
167 | void vm_area_unmap(vm_area_t *a, vm_t *m) |
171 | void vm_area_unmap(vm_area_t *a, vm_t *m) |
168 | { |
172 | { |
169 | int i; |
173 | int i; |
170 | pri_t pri; |
174 | pri_t pri; |
171 | 175 | ||
172 | pri = cpu_priority_high(); |
176 | pri = cpu_priority_high(); |
173 | spinlock_lock(&m->lock); |
177 | spinlock_lock(&m->lock); |
174 | spinlock_lock(&a->lock); |
178 | spinlock_lock(&a->lock); |
175 | 179 | ||
176 | ASSERT(m->ptl0); |
180 | ASSERT(m->ptl0); |
177 | for (i=0; i<a->size; i++) |
181 | for (i=0; i<a->size; i++) |
178 | map_page_to_frame(a->address + i*PAGE_SIZE, 0, PAGE_NOT_PRESENT, (__address) m->ptl0); |
182 | map_page_to_frame(a->address + i*PAGE_SIZE, 0, PAGE_NOT_PRESENT, (__address) m->ptl0); |
179 | 183 | ||
180 | spinlock_unlock(&a->lock); |
184 | spinlock_unlock(&a->lock); |
181 | spinlock_unlock(&m->lock); |
185 | spinlock_unlock(&m->lock); |
182 | cpu_priority_restore(pri); |
186 | cpu_priority_restore(pri); |
183 | } |
187 | } |
184 | 188 | ||
185 | void vm_install(vm_t *m) |
189 | void vm_install(vm_t *m) |
186 | { |
190 | { |
187 | link_t *l; |
191 | link_t *l; |
188 | pri_t pri; |
192 | pri_t pri; |
189 | 193 | ||
190 | pri = cpu_priority_high(); |
194 | pri = cpu_priority_high(); |
191 | 195 | ||
192 | tlb_shootdown_start(); |
196 | tlb_shootdown_start(); |
193 | spinlock_lock(&m->lock); |
197 | spinlock_lock(&m->lock); |
194 | 198 | ||
195 | ASSERT(m->ptl0); |
199 | ASSERT(m->ptl0); |
196 | SET_PTL0_ADDRESS(m->ptl0); |
200 | SET_PTL0_ADDRESS(m->ptl0); |
197 | 201 | ||
198 | spinlock_unlock(&m->lock); |
202 | spinlock_unlock(&m->lock); |
199 | tlb_shootdown_finalize(); |
203 | tlb_shootdown_finalize(); |
200 | 204 | ||
201 | cpu_priority_restore(pri); |
205 | cpu_priority_restore(pri); |
- | 206 | ||
- | 207 | vm_install_arch(m); |
|
- | 208 | ||
- | 209 | VM = m; |
|
202 | } |
210 | } |
203 | 211 |