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