Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | #include <mm/vm.h> |
||
30 | #include <mm/page.h> |
||
31 | #include <mm/frame.h> |
||
5 | jermar | 32 | #include <mm/tlb.h> |
35 | jermar | 33 | #include <mm/heap.h> |
1 | jermar | 34 | #include <arch/mm/page.h> |
35 | #include <arch/types.h> |
||
36 | #include <typedefs.h> |
||
37 | #include <synch/spinlock.h> |
||
38 | #include <config.h> |
||
39 | #include <list.h> |
||
40 | #include <panic.h> |
||
115 | jermar | 41 | #include <arch/asm.h> |
1 | jermar | 42 | |
43 | vm_t *vm_create(void) |
||
44 | { |
||
45 | vm_t *m; |
||
46 | |||
47 | m = (vm_t *) malloc(sizeof(vm_t)); |
||
48 | if (m) { |
||
49 | spinlock_initialize(&m->lock); |
||
50 | list_initialize(&m->vm_area_head); |
||
121 | jermar | 51 | m->ptl0 = NULL; |
1 | jermar | 52 | } |
53 | |||
54 | return m; |
||
55 | } |
||
56 | |||
57 | void vm_destroy(vm_t *m) |
||
58 | { |
||
59 | } |
||
60 | |||
72 | decky | 61 | vm_area_t *vm_area_create(vm_t *m, vm_type_t type, size_t size, __address addr) |
1 | jermar | 62 | { |
63 | pri_t pri; |
||
64 | vm_area_t *a; |
||
65 | |||
66 | if (addr % PAGE_SIZE) |
||
68 | decky | 67 | panic("addr not aligned to a page boundary"); |
1 | jermar | 68 | |
69 | pri = cpu_priority_high(); |
||
70 | spinlock_lock(&m->lock); |
||
71 | |||
72 | /* |
||
73 | * TODO: test vm_area which is to be created doesn't overlap with an existing one. |
||
74 | */ |
||
75 | |||
76 | a = (vm_area_t *) malloc(sizeof(vm_area_t)); |
||
77 | if (a) { |
||
78 | int i; |
||
79 | |||
80 | a->mapping = (__address *) malloc(size * sizeof(__address)); |
||
81 | if (!a->mapping) { |
||
82 | free(a); |
||
83 | spinlock_unlock(&m->lock); |
||
84 | cpu_priority_restore(pri); |
||
85 | return NULL; |
||
86 | } |
||
87 | |||
88 | for (i=0; i<size; i++) |
||
89 | a->mapping[i] = frame_alloc(0); |
||
90 | |||
91 | spinlock_initialize(&a->lock); |
||
92 | |||
93 | link_initialize(&a->link); |
||
94 | a->type = type; |
||
95 | a->size = size; |
||
96 | a->address = addr; |
||
97 | |||
98 | list_append(&a->link, &m->vm_area_head); |
||
99 | |||
100 | } |
||
101 | |||
102 | spinlock_unlock(&m->lock); |
||
103 | cpu_priority_restore(pri); |
||
104 | |||
105 | return a; |
||
106 | } |
||
107 | |||
108 | void vm_area_destroy(vm_area_t *a) |
||
109 | { |
||
110 | } |
||
111 | |||
112 | void vm_area_map(vm_area_t *a) |
||
113 | { |
||
114 | int i, flags; |
||
115 | pri_t pri; |
||
116 | |||
117 | pri = cpu_priority_high(); |
||
118 | spinlock_lock(&a->lock); |
||
119 | |||
120 | switch (a->type) { |
||
121 | case VMA_TEXT: |
||
122 | flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
||
123 | break; |
||
124 | case VMA_DATA: |
||
125 | case VMA_STACK: |
||
126 | flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE; |
||
127 | break; |
||
128 | default: |
||
68 | decky | 129 | panic("unexpected vm_type_t %d", a->type); |
1 | jermar | 130 | } |
131 | |||
132 | for (i=0; i<a->size; i++) |
||
133 | map_page_to_frame(a->address + i*PAGE_SIZE, a->mapping[i], flags, 0); |
||
134 | |||
135 | spinlock_unlock(&a->lock); |
||
136 | cpu_priority_restore(pri); |
||
137 | } |
||
138 | |||
139 | void vm_area_unmap(vm_area_t *a) |
||
140 | { |
||
141 | int i; |
||
142 | pri_t pri; |
||
143 | |||
144 | pri = cpu_priority_high(); |
||
145 | spinlock_lock(&a->lock); |
||
146 | |||
147 | for (i=0; i<a->size; i++) |
||
148 | map_page_to_frame(a->address + i*PAGE_SIZE, 0, PAGE_NOT_PRESENT, 0); |
||
5 | jermar | 149 | |
1 | jermar | 150 | spinlock_unlock(&a->lock); |
151 | cpu_priority_restore(pri); |
||
152 | } |
||
153 | |||
154 | void vm_install(vm_t *m) |
||
155 | { |
||
156 | link_t *l; |
||
157 | pri_t pri; |
||
158 | |||
159 | pri = cpu_priority_high(); |
||
160 | spinlock_lock(&m->lock); |
||
161 | |||
162 | for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next) |
||
163 | vm_area_map(list_get_instance(l, vm_area_t, link)); |
||
164 | |||
165 | spinlock_unlock(&m->lock); |
||
166 | cpu_priority_restore(pri); |
||
167 | } |
||
168 | |||
169 | void vm_uninstall(vm_t *m) |
||
170 | { |
||
171 | link_t *l; |
||
172 | pri_t pri; |
||
173 | |||
174 | pri = cpu_priority_high(); |
||
5 | jermar | 175 | |
6 | jermar | 176 | tlb_shootdown_start(); |
5 | jermar | 177 | |
1 | jermar | 178 | spinlock_lock(&m->lock); |
179 | |||
180 | for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next) |
||
181 | vm_area_unmap(list_get_instance(l, vm_area_t, link)); |
||
182 | |||
183 | spinlock_unlock(&m->lock); |
||
5 | jermar | 184 | |
6 | jermar | 185 | tlb_shootdown_finalize(); |
5 | jermar | 186 | |
1 | jermar | 187 | cpu_priority_restore(pri); |
188 | } |