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