Rev 219 | Rev 368 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 219 | Rev 367 | ||
---|---|---|---|
Line 37... | Line 37... | ||
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 | #include <debug.h> |
|
42 | 43 | ||
43 | #include <synch/spinlock.h> |
44 | #include <synch/spinlock.h> |
44 | 45 | ||
45 | #include <arch/asm.h> |
46 | #include <arch/asm.h> |
46 | #include <arch.h> |
47 | #include <arch.h> |
Line 51... | Line 52... | ||
51 | __u8 *frame_bitmap; |
52 | __u8 *frame_bitmap; |
52 | count_t frame_bitmap_octets; |
53 | count_t frame_bitmap_octets; |
53 | 54 | ||
54 | static spinlock_t framelock; |
55 | static spinlock_t framelock; |
55 | 56 | ||
- | 57 | spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
|
- | 58 | link_t zone_head; /**< list of all zones in the system */ |
|
- | 59 | ||
- | 60 | ||
56 | void frame_init(void) |
61 | void frame_init(void) |
57 | { |
62 | { |
58 | if (config.cpu_active == 1) { |
63 | if (config.cpu_active == 1) { |
- | 64 | zone_init(); |
|
59 | 65 | ||
60 | /* |
66 | /* |
61 | * The bootstrap processor will allocate all necessary memory for frame allocation. |
67 | * The bootstrap processor will allocate all necessary memory for frame allocation. |
62 | */ |
68 | */ |
63 | 69 | ||
Line 196... | Line 202... | ||
196 | start /= FRAME_SIZE; |
202 | start /= FRAME_SIZE; |
197 | stop /= FRAME_SIZE; |
203 | stop /= FRAME_SIZE; |
198 | for (a = start; a <= stop; a++) |
204 | for (a = start; a <= stop; a++) |
199 | frame_not_free(a * FRAME_SIZE); |
205 | frame_not_free(a * FRAME_SIZE); |
200 | } |
206 | } |
- | 207 | ||
- | 208 | /** Initialize zonekeeping |
|
- | 209 | * |
|
- | 210 | * Initialize zonekeeping. |
|
- | 211 | */ |
|
- | 212 | void zone_init(void) |
|
- | 213 | { |
|
- | 214 | spinlock_initialize(&zone_head_lock); |
|
- | 215 | list_initialize(&zone_head); |
|
- | 216 | } |
|
- | 217 | ||
- | 218 | /** Create frame zone |
|
- | 219 | * |
|
- | 220 | * Create new frame zone. |
|
- | 221 | * |
|
- | 222 | * @param start Physical address of the first frame within the zone. |
|
- | 223 | * @param size Size of the zone. Must be a multiple of FRAME_SIZE. |
|
- | 224 | * @param flags Zone flags. |
|
- | 225 | * |
|
- | 226 | * @return Initialized zone. |
|
- | 227 | */ |
|
- | 228 | zone_t *zone_create(__address start, size_t size, int flags) |
|
- | 229 | { |
|
- | 230 | zone_t *z; |
|
- | 231 | count_t cnt; |
|
- | 232 | int i; |
|
- | 233 | ||
- | 234 | ASSERT(start % FRAME_SIZE == 0); |
|
- | 235 | ASSERT(size % FRAME_SIZE == 0); |
|
- | 236 | ||
- | 237 | cnt = size / FRAME_SIZE; |
|
- | 238 | ||
- | 239 | z = (zone_t *) malloc(sizeof(zone_t)); |
|
- | 240 | if (z) { |
|
- | 241 | link_initialize(&z->link); |
|
- | 242 | spinlock_initialize(&z->lock); |
|
- | 243 | ||
- | 244 | z->base = start; |
|
- | 245 | z->flags = flags; |
|
- | 246 | ||
- | 247 | z->free_count = cnt; |
|
- | 248 | list_initialize(&z->free_head); |
|
- | 249 | ||
- | 250 | z->busy_count = 0; |
|
- | 251 | list_initialize(&z->busy_head); |
|
- | 252 | ||
- | 253 | z->frames = (frame_t *) malloc(cnt * sizeof(frame_t)); |
|
- | 254 | if (!z->frames) { |
|
- | 255 | free(z); |
|
- | 256 | return NULL; |
|
- | 257 | } |
|
- | 258 | ||
- | 259 | for (i = 0; i<cnt; i++) { |
|
- | 260 | frame_initialize(&z->frames[i], z); |
|
- | 261 | list_append(&z->frames[i].link, &z->free_head); |
|
- | 262 | } |
|
- | 263 | ||
- | 264 | } |
|
- | 265 | ||
- | 266 | return z; |
|
- | 267 | } |
|
- | 268 | ||
- | 269 | /** Attach frame zone |
|
- | 270 | * |
|
- | 271 | * Attach frame zone to zone list. |
|
- | 272 | * |
|
- | 273 | * @param zone Zone to be attached. |
|
- | 274 | */ |
|
- | 275 | void zone_attach(zone_t *zone) |
|
- | 276 | { |
|
- | 277 | pri_t pri; |
|
- | 278 | ||
- | 279 | pri = cpu_priority_high(); |
|
- | 280 | spinlock_lock(&zone_head_lock); |
|
- | 281 | ||
- | 282 | list_append(&zone->link, &zone_head); |
|
- | 283 | ||
- | 284 | spinlock_unlock(&zone_head_lock); |
|
- | 285 | cpu_priority_restore(pri); |
|
- | 286 | } |
|
- | 287 | ||
- | 288 | /** Initialize frame structure |
|
- | 289 | * |
|
- | 290 | * Initialize frame structure. |
|
- | 291 | * |
|
- | 292 | * @param frame Frame structure to be initialized. |
|
- | 293 | * @param zone Host frame zone. |
|
- | 294 | */ |
|
- | 295 | void frame_initialize(frame_t *frame, zone_t *zone) |
|
- | 296 | { |
|
- | 297 | frame->refcount = 0; |
|
- | 298 | link_initialize(&frame->link); |
|
- | 299 | frame->zone = zone; |
|
- | 300 | } |
|
- | 301 | ||
- | 302 | /** Get address of physical frame from its frame structure |
|
- | 303 | * |
|
- | 304 | * Get address of physical frame from its frame structure. |
|
- | 305 | * |
|
- | 306 | * @param frame Frame structure of the queried frame address. |
|
- | 307 | * |
|
- | 308 | * @return Address of frame associated with the argument. |
|
- | 309 | */ |
|
- | 310 | __address frame_get_address(frame_t *frame) |
|
- | 311 | { |
|
- | 312 | __address v; |
|
- | 313 | zone_t *z; |
|
- | 314 | pri_t pri; |
|
- | 315 | ||
- | 316 | z = frame->zone; |
|
- | 317 | ||
- | 318 | pri = cpu_priority_high(); |
|
- | 319 | spinlock_lock(&z->lock); |
|
- | 320 | ||
- | 321 | v = z->base + (frame - z->frames) * FRAME_SIZE; |
|
- | 322 | ||
- | 323 | if (z->flags & FRAME_KA) |
|
- | 324 | v = PA2KA(v); |
|
- | 325 | ||
- | 326 | spinlock_unlock(&z->lock); |
|
- | 327 | cpu_priority_restore(pri); |
|
- | 328 | ||
- | 329 | return v; |
|
- | 330 | } |