Rev 68 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 68 | Rev 72 | ||
---|---|---|---|
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 | 41 | ||
42 | vm_t *vm_create(void) |
42 | vm_t *vm_create(void) |
43 | { |
43 | { |
44 | vm_t *m; |
44 | vm_t *m; |
45 | 45 | ||
46 | m = (vm_t *) malloc(sizeof(vm_t)); |
46 | m = (vm_t *) malloc(sizeof(vm_t)); |
47 | if (m) { |
47 | if (m) { |
48 | spinlock_initialize(&m->lock); |
48 | spinlock_initialize(&m->lock); |
49 | list_initialize(&m->vm_area_head); |
49 | list_initialize(&m->vm_area_head); |
50 | } |
50 | } |
51 | 51 | ||
52 | return m; |
52 | return m; |
53 | } |
53 | } |
54 | 54 | ||
55 | void vm_destroy(vm_t *m) |
55 | void vm_destroy(vm_t *m) |
56 | { |
56 | { |
57 | } |
57 | } |
58 | 58 | ||
59 | vm_area_t *vm_area_create(vm_t *m, vm_type_t type, int size, __address addr) |
59 | vm_area_t *vm_area_create(vm_t *m, vm_type_t type, size_t size, __address addr) |
60 | { |
60 | { |
61 | pri_t pri; |
61 | pri_t pri; |
62 | vm_area_t *a; |
62 | vm_area_t *a; |
63 | 63 | ||
64 | if (addr % PAGE_SIZE) |
64 | if (addr % PAGE_SIZE) |
65 | panic("addr not aligned to a page boundary"); |
65 | panic("addr not aligned to a page boundary"); |
66 | 66 | ||
67 | pri = cpu_priority_high(); |
67 | pri = cpu_priority_high(); |
68 | spinlock_lock(&m->lock); |
68 | spinlock_lock(&m->lock); |
69 | 69 | ||
70 | /* |
70 | /* |
71 | * TODO: test vm_area which is to be created doesn't overlap with an existing one. |
71 | * TODO: test vm_area which is to be created doesn't overlap with an existing one. |
72 | */ |
72 | */ |
73 | 73 | ||
74 | a = (vm_area_t *) malloc(sizeof(vm_area_t)); |
74 | a = (vm_area_t *) malloc(sizeof(vm_area_t)); |
75 | if (a) { |
75 | if (a) { |
76 | int i; |
76 | int i; |
77 | 77 | ||
78 | a->mapping = (__address *) malloc(size * sizeof(__address)); |
78 | a->mapping = (__address *) malloc(size * sizeof(__address)); |
79 | if (!a->mapping) { |
79 | if (!a->mapping) { |
80 | free(a); |
80 | free(a); |
81 | spinlock_unlock(&m->lock); |
81 | spinlock_unlock(&m->lock); |
82 | cpu_priority_restore(pri); |
82 | cpu_priority_restore(pri); |
83 | return NULL; |
83 | return NULL; |
84 | } |
84 | } |
85 | 85 | ||
86 | for (i=0; i<size; i++) |
86 | for (i=0; i<size; i++) |
87 | a->mapping[i] = frame_alloc(0); |
87 | a->mapping[i] = frame_alloc(0); |
88 | 88 | ||
89 | spinlock_initialize(&a->lock); |
89 | spinlock_initialize(&a->lock); |
90 | 90 | ||
91 | link_initialize(&a->link); |
91 | link_initialize(&a->link); |
92 | a->type = type; |
92 | a->type = type; |
93 | a->size = size; |
93 | a->size = size; |
94 | a->address = addr; |
94 | a->address = addr; |
95 | 95 | ||
96 | list_append(&a->link, &m->vm_area_head); |
96 | list_append(&a->link, &m->vm_area_head); |
97 | 97 | ||
98 | } |
98 | } |
99 | 99 | ||
100 | spinlock_unlock(&m->lock); |
100 | spinlock_unlock(&m->lock); |
101 | cpu_priority_restore(pri); |
101 | cpu_priority_restore(pri); |
102 | 102 | ||
103 | return a; |
103 | return a; |
104 | } |
104 | } |
105 | 105 | ||
106 | void vm_area_destroy(vm_area_t *a) |
106 | void vm_area_destroy(vm_area_t *a) |
107 | { |
107 | { |
108 | } |
108 | } |
109 | 109 | ||
110 | void vm_area_map(vm_area_t *a) |
110 | void vm_area_map(vm_area_t *a) |
111 | { |
111 | { |
112 | int i, flags; |
112 | int i, flags; |
113 | pri_t pri; |
113 | pri_t pri; |
114 | 114 | ||
115 | pri = cpu_priority_high(); |
115 | pri = cpu_priority_high(); |
116 | spinlock_lock(&a->lock); |
116 | spinlock_lock(&a->lock); |
117 | 117 | ||
118 | switch (a->type) { |
118 | switch (a->type) { |
119 | case VMA_TEXT: |
119 | case VMA_TEXT: |
120 | flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
120 | flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
121 | break; |
121 | break; |
122 | case VMA_DATA: |
122 | case VMA_DATA: |
123 | case VMA_STACK: |
123 | case VMA_STACK: |
124 | flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
124 | flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
125 | break; |
125 | break; |
126 | default: |
126 | default: |
127 | panic("unexpected vm_type_t %d", a->type); |
127 | panic("unexpected vm_type_t %d", a->type); |
128 | } |
128 | } |
129 | 129 | ||
130 | for (i=0; i<a->size; i++) |
130 | for (i=0; i<a->size; i++) |
131 | map_page_to_frame(a->address + i*PAGE_SIZE, a->mapping[i], flags, 0); |
131 | map_page_to_frame(a->address + i*PAGE_SIZE, a->mapping[i], flags, 0); |
132 | 132 | ||
133 | spinlock_unlock(&a->lock); |
133 | spinlock_unlock(&a->lock); |
134 | cpu_priority_restore(pri); |
134 | cpu_priority_restore(pri); |
135 | } |
135 | } |
136 | 136 | ||
137 | void vm_area_unmap(vm_area_t *a) |
137 | void vm_area_unmap(vm_area_t *a) |
138 | { |
138 | { |
139 | int i; |
139 | int i; |
140 | pri_t pri; |
140 | pri_t pri; |
141 | 141 | ||
142 | pri = cpu_priority_high(); |
142 | pri = cpu_priority_high(); |
143 | spinlock_lock(&a->lock); |
143 | spinlock_lock(&a->lock); |
144 | 144 | ||
145 | for (i=0; i<a->size; i++) |
145 | for (i=0; i<a->size; i++) |
146 | map_page_to_frame(a->address + i*PAGE_SIZE, 0, PAGE_NOT_PRESENT, 0); |
146 | map_page_to_frame(a->address + i*PAGE_SIZE, 0, PAGE_NOT_PRESENT, 0); |
147 | 147 | ||
148 | spinlock_unlock(&a->lock); |
148 | spinlock_unlock(&a->lock); |
149 | cpu_priority_restore(pri); |
149 | cpu_priority_restore(pri); |
150 | } |
150 | } |
151 | 151 | ||
152 | void vm_install(vm_t *m) |
152 | void vm_install(vm_t *m) |
153 | { |
153 | { |
154 | link_t *l; |
154 | link_t *l; |
155 | pri_t pri; |
155 | pri_t pri; |
156 | 156 | ||
157 | pri = cpu_priority_high(); |
157 | pri = cpu_priority_high(); |
158 | spinlock_lock(&m->lock); |
158 | spinlock_lock(&m->lock); |
159 | 159 | ||
160 | for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next) |
160 | for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next) |
161 | vm_area_map(list_get_instance(l, vm_area_t, link)); |
161 | vm_area_map(list_get_instance(l, vm_area_t, link)); |
162 | 162 | ||
163 | spinlock_unlock(&m->lock); |
163 | spinlock_unlock(&m->lock); |
164 | cpu_priority_restore(pri); |
164 | cpu_priority_restore(pri); |
165 | } |
165 | } |
166 | 166 | ||
167 | void vm_uninstall(vm_t *m) |
167 | void vm_uninstall(vm_t *m) |
168 | { |
168 | { |
169 | link_t *l; |
169 | link_t *l; |
170 | pri_t pri; |
170 | pri_t pri; |
171 | 171 | ||
172 | pri = cpu_priority_high(); |
172 | pri = cpu_priority_high(); |
173 | 173 | ||
174 | tlb_shootdown_start(); |
174 | tlb_shootdown_start(); |
175 | 175 | ||
176 | spinlock_lock(&m->lock); |
176 | spinlock_lock(&m->lock); |
177 | 177 | ||
178 | for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next) |
178 | for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next) |
179 | vm_area_unmap(list_get_instance(l, vm_area_t, link)); |
179 | vm_area_unmap(list_get_instance(l, vm_area_t, link)); |
180 | 180 | ||
181 | spinlock_unlock(&m->lock); |
181 | spinlock_unlock(&m->lock); |
182 | 182 | ||
183 | tlb_shootdown_finalize(); |
183 | tlb_shootdown_finalize(); |
184 | 184 | ||
185 | cpu_priority_restore(pri); |
185 | cpu_priority_restore(pri); |
186 | } |
186 | } |
187 | 187 |