Rev 115 | Rev 195 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 115 | Rev 125 | ||
---|---|---|---|
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 <arch/types.h> |
29 | #include <arch/types.h> |
30 | #include <func.h> |
30 | #include <func.h> |
31 | 31 | ||
32 | #include <mm/heap.h> |
32 | #include <mm/heap.h> |
33 | #include <mm/frame.h> |
33 | #include <mm/frame.h> |
34 | #include <mm/page.h> |
34 | #include <mm/page.h> |
35 | #include <mm/vm.h> |
35 | #include <mm/vm.h> |
36 | #include <arch/mm/page.h> |
36 | #include <arch/mm/page.h> |
37 | 37 | ||
38 | #include <config.h> |
38 | #include <config.h> |
39 | #include <memstr.h> |
39 | #include <memstr.h> |
40 | 40 | ||
41 | #include <panic.h> |
41 | #include <panic.h> |
42 | 42 | ||
43 | #include <synch/spinlock.h> |
43 | #include <synch/spinlock.h> |
44 | 44 | ||
45 | #include <arch/asm.h> |
45 | #include <arch/asm.h> |
46 | 46 | ||
47 | count_t frames = 0; |
47 | count_t frames = 0; |
48 | count_t frames_free; |
48 | count_t frames_free; |
49 | 49 | ||
50 | __u8 *frame_bitmap; |
50 | __u8 *frame_bitmap; |
51 | count_t frame_bitmap_octets; |
51 | count_t frame_bitmap_octets; |
52 | 52 | ||
53 | static spinlock_t framelock; |
53 | static spinlock_t framelock; |
54 | 54 | ||
55 | void frame_init(void) |
55 | void frame_init(void) |
56 | { |
56 | { |
57 | if (config.cpu_active == 1) { |
57 | if (config.cpu_active == 1) { |
58 | 58 | ||
59 | /* |
59 | /* |
60 | * The bootstrap processor will allocate all necessary memory for frame allocation. |
60 | * The bootstrap processor will allocate all necessary memory for frame allocation. |
61 | */ |
61 | */ |
62 | 62 | ||
63 | frames = config.memory_size / FRAME_SIZE; |
63 | frames = config.memory_size / FRAME_SIZE; |
64 | frame_bitmap_octets = frames / 8 + (frames % 8 > 0); |
64 | frame_bitmap_octets = frames / 8 + (frames % 8 > 0); |
65 | frame_bitmap = (__u8 *) malloc(frame_bitmap_octets); |
65 | frame_bitmap = (__u8 *) malloc(frame_bitmap_octets); |
66 | if (!frame_bitmap) |
66 | if (!frame_bitmap) |
67 | panic("malloc/frame_bitmap\n"); |
67 | panic("malloc/frame_bitmap\n"); |
68 | 68 | ||
69 | /* |
69 | /* |
70 | * Mark all frames free. |
70 | * Mark all frames free. |
71 | */ |
71 | */ |
72 | memsetb((__address) frame_bitmap, frame_bitmap_octets, 0); |
72 | memsetb((__address) frame_bitmap, frame_bitmap_octets, 0); |
73 | frames_free = frames; |
73 | frames_free = frames; |
74 | } |
74 | } |
75 | 75 | ||
76 | /* |
76 | /* |
77 | * No frame allocations/reservations prior this point. |
77 | * No frame allocations/reservations prior this point. |
78 | */ |
78 | */ |
79 | 79 | ||
80 | frame_arch_init(); |
80 | frame_arch_init(); |
81 | 81 | ||
82 | if (config.cpu_active == 1) { |
82 | if (config.cpu_active == 1) { |
83 | /* |
83 | /* |
84 | * Create the memory address space map. Marked frames and frame |
84 | * Create the memory address space map. Marked frames and frame |
85 | * regions cannot be used for allocation. |
85 | * regions cannot be used for allocation. |
86 | */ |
86 | */ |
87 | frame_region_not_free(config.base, config.base + config.kernel_size); |
87 | frame_region_not_free(config.base, config.base + config.kernel_size); |
88 | } |
88 | } |
89 | } |
89 | } |
90 | 90 | ||
91 | /* |
91 | /* |
92 | * Allocate a frame. |
92 | * Allocate a frame. |
93 | */ |
93 | */ |
94 | __address frame_alloc(int flags) |
94 | __address frame_alloc(int flags) |
95 | { |
95 | { |
96 | int i; |
96 | int i; |
97 | pri_t pri; |
97 | pri_t pri; |
98 | 98 | ||
99 | loop: |
99 | loop: |
100 | pri = cpu_priority_high(); |
100 | pri = cpu_priority_high(); |
101 | spinlock_lock(&framelock); |
101 | spinlock_lock(&framelock); |
102 | if (frames_free) { |
102 | if (frames_free) { |
103 | for (i=0; i < frames; i++) { |
103 | for (i=0; i < frames; i++) { |
104 | int m, n; |
104 | int m, n; |
105 | 105 | ||
106 | m = i / 8; |
106 | m = i / 8; |
107 | n = i % 8; |
107 | n = i % 8; |
108 | 108 | ||
109 | if ((frame_bitmap[m] & (1<<n)) == 0) { |
109 | if ((frame_bitmap[m] & (1<<n)) == 0) { |
110 | frame_bitmap[m] |= (1<<n); |
110 | frame_bitmap[m] |= (1<<n); |
111 | frames_free--; |
111 | frames_free--; |
112 | spinlock_unlock(&framelock); |
112 | spinlock_unlock(&framelock); |
113 | cpu_priority_restore(pri); |
113 | cpu_priority_restore(pri); |
114 | if (flags & FRAME_KA) return PA2KA(i*FRAME_SIZE); |
114 | if (flags & FRAME_KA) return PA2KA(i*FRAME_SIZE); |
115 | return i*FRAME_SIZE; |
115 | return i*FRAME_SIZE; |
116 | } |
116 | } |
117 | } |
117 | } |
118 | panic("frames_free inconsistent (%d)\n", frames_free); |
118 | panic("frames_free inconsistent (%d)\n", frames_free); |
119 | } |
119 | } |
120 | spinlock_unlock(&framelock); |
120 | spinlock_unlock(&framelock); |
121 | cpu_priority_restore(pri); |
121 | cpu_priority_restore(pri); |
122 | 122 | ||
123 | if (flags & FRAME_PANIC) |
123 | if (flags & FRAME_PANIC) |
124 | panic("unable to allocate frame\n"); |
124 | panic("unable to allocate frame\n"); |
125 | 125 | ||
126 | /* TODO: implement sleeping logic here */ |
126 | /* TODO: implement sleeping logic here */ |
127 | panic("sleep not supported\n"); |
127 | panic("sleep not supported\n"); |
128 | 128 | ||
129 | goto loop; |
129 | goto loop; |
130 | } |
130 | } |
131 | 131 | ||
132 | /* |
132 | /* |
133 | * Free a frame. |
133 | * Free a frame. |
134 | */ |
134 | */ |
135 | void frame_free(__address addr) |
135 | void frame_free(__address addr) |
136 | { |
136 | { |
137 | pri_t pri; |
137 | pri_t pri; |
138 | __u32 frame; |
138 | __u32 frame; |
139 | 139 | ||
140 | pri = cpu_priority_high(); |
140 | pri = cpu_priority_high(); |
141 | spinlock_lock(&framelock); |
141 | spinlock_lock(&framelock); |
142 | 142 | ||
143 | frame = IS_KA(addr) ? KA2PA(addr) : addr; |
143 | frame = IS_KA(addr) ? KA2PA(addr) : addr; |
144 | frame /= FRAME_SIZE; |
144 | frame /= FRAME_SIZE; |
145 | if (frame < frames) { |
145 | if (frame < frames) { |
146 | int m, n; |
146 | int m, n; |
147 | 147 | ||
148 | m = frame / 8; |
148 | m = frame / 8; |
149 | n = frame % 8; |
149 | n = frame % 8; |
150 | 150 | ||
151 | if (frame_bitmap[m] & (1<<n)) { |
151 | if (frame_bitmap[m] & (1<<n)) { |
152 | frame_bitmap[m] &= ~(1<<n); |
152 | frame_bitmap[m] &= ~(1<<n); |
153 | frames_free++; |
153 | frames_free++; |
154 | } |
154 | } |
155 | else panic("frame already free\n"); |
155 | else panic("frame already free\n"); |
156 | } |
156 | } |
157 | else panic("frame number too big\n"); |
157 | else panic("frame number too big\n"); |
158 | 158 | ||
159 | spinlock_unlock(&framelock); |
159 | spinlock_unlock(&framelock); |
160 | cpu_priority_restore(pri); |
160 | cpu_priority_restore(pri); |
161 | } |
161 | } |
162 | 162 | ||
163 | /* |
163 | /* |
164 | * Don't use this function for normal allocation. Use frame_alloc() instead. |
164 | * Don't use this function for normal allocation. Use frame_alloc() instead. |
165 | * Use this function to declare that some special frame is not free. |
165 | * Use this function to declare that some special frame is not free. |
166 | */ |
166 | */ |
167 | void frame_not_free(__address addr) |
167 | void frame_not_free(__address addr) |
168 | { |
168 | { |
169 | pri_t pri; |
169 | pri_t pri; |
170 | __u32 frame; |
170 | __u32 frame; |
171 | 171 | ||
172 | pri = cpu_priority_high(); |
172 | pri = cpu_priority_high(); |
173 | spinlock_lock(&framelock); |
173 | spinlock_lock(&framelock); |
174 | frame = IS_KA(addr) ? KA2PA(addr) : addr; |
174 | frame = IS_KA(addr) ? KA2PA(addr) : addr; |
175 | frame /= FRAME_SIZE; |
175 | frame /= FRAME_SIZE; |
176 | if (frame < frames) { |
176 | if (frame < frames) { |
177 | int m, n; |
177 | int m, n; |
178 | 178 | ||
179 | m = frame / 8; |
179 | m = frame / 8; |
180 | n = frame % 8; |
180 | n = frame % 8; |
181 | 181 | ||
182 | if ((frame_bitmap[m] & (1<<n)) == 0) { |
182 | if ((frame_bitmap[m] & (1<<n)) == 0) { |
183 | frame_bitmap[m] |= (1<<n); |
183 | frame_bitmap[m] |= (1<<n); |
184 | frames_free--; |
184 | frames_free--; |
185 | } |
185 | } |
186 | } |
186 | } |
187 | spinlock_unlock(&framelock); |
187 | spinlock_unlock(&framelock); |
188 | cpu_priority_restore(pri); |
188 | cpu_priority_restore(pri); |
189 | } |
189 | } |
190 | 190 | ||
191 | void frame_region_not_free(__address start, __address stop) |
191 | void frame_region_not_free(__address start, __address stop) |
192 | { |
192 | { |
193 | __u32 i; |
193 | __u32 i; |
194 | 194 | ||
195 | start /= FRAME_SIZE; |
195 | start /= FRAME_SIZE; |
196 | stop /= FRAME_SIZE; |
196 | stop /= FRAME_SIZE; |
197 | for (i = start; i <= stop; i++) |
197 | for (i = start; i <= stop; i++) |
198 | frame_not_free(i * FRAME_SIZE); |
198 | frame_not_free(i * FRAME_SIZE); |
199 | } |
199 | } |
200 | 200 |