Subversion Repositories HelenOS-historic

Rev

Rev 534 | Rev 537 | Go to most recent revision | Details | Compare with Previous | 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
 
368 jermar 29
#include <typedefs.h>
1 jermar 30
#include <arch/types.h>
31
#include <mm/heap.h>
32
#include <mm/frame.h>
33
#include <mm/vm.h>
34
#include <panic.h>
367 jermar 35
#include <debug.h>
368 jermar 36
#include <list.h>
1 jermar 37
#include <synch/spinlock.h>
115 jermar 38
#include <arch/asm.h>
195 vana 39
#include <arch.h>
533 bondari 40
#include <print.h>
536 bondari 41
#include <align.h>
115 jermar 42
 
367 jermar 43
spinlock_t zone_head_lock;       /**< this lock protects zone_head list */
44
link_t zone_head;                /**< list of all zones in the system */
45
 
533 bondari 46
region_t zone_blacklist[ZONE_BLACKLIST_SIZE];
47
count_t zone_blacklist_count = 0;
48
 
479 bondari 49
static struct buddy_system_operations  zone_buddy_system_operations = {
50
    .find_buddy = zone_buddy_find_buddy,
51
    .bisect = zone_buddy_bisect,
52
    .coalesce = zone_buddy_coalesce,
53
    .set_order = zone_buddy_set_order,
54
    .get_order = zone_buddy_get_order,
533 bondari 55
    .mark_busy = zone_buddy_mark_busy,
479 bondari 56
};
57
 
368 jermar 58
/** Initialize physical memory management
59
 *
60
 * Initialize physical memory managemnt.
61
 */
1 jermar 62
void frame_init(void)
63
{
125 jermar 64
    if (config.cpu_active == 1) {
367 jermar 65
        zone_init();
536 bondari 66
        frame_region_not_free(config.base, config.kernel_size);
1 jermar 67
    }
68
 
69
    frame_arch_init();
70
}
71
 
368 jermar 72
/** Allocate a frame
73
 *
74
 * Allocate a frame of physical memory.
75
 *
76
 * @param flags Flags for host zone selection and address processing.
77
 *
78
 * @return Allocated frame.
1 jermar 79
 */
533 bondari 80
__address frame_alloc(int flags, __u8 order)
1 jermar 81
{
413 jermar 82
    ipl_t ipl;
368 jermar 83
    link_t *cur, *tmp;
84
    zone_t *z;
85
    zone_t *zone = NULL;
86
    frame_t *frame = NULL;
87
    __address v;
1 jermar 88
 
89
loop:
413 jermar 90
    ipl = interrupts_disable();
368 jermar 91
    spinlock_lock(&zone_head_lock);
533 bondari 92
 
368 jermar 93
    /*
94
     * First, find suitable frame zone.
95
     */
96
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
97
        z = list_get_instance(cur, zone_t, link);
1 jermar 98
 
368 jermar 99
        spinlock_lock(&z->lock);
533 bondari 100
 
101
        /* Check if the zone has 2^order frames area available  */
102
        if (buddy_system_can_alloc(z->buddy_system, order)) {
368 jermar 103
            zone = z;
104
            break;
1 jermar 105
        }
533 bondari 106
 
368 jermar 107
        spinlock_unlock(&z->lock);
1 jermar 108
    }
368 jermar 109
 
110
    if (!zone) {
111
        if (flags & FRAME_PANIC)
112
            panic("Can't allocate frame.\n");
113
 
114
        /*
115
         * TODO: Sleep until frames are available again.
116
         */
117
        spinlock_unlock(&zone_head_lock);
413 jermar 118
        interrupts_restore(ipl);
1 jermar 119
 
368 jermar 120
        panic("Sleep not implemented.\n");
121
        goto loop;
122
    }
1 jermar 123
 
368 jermar 124
 
533 bondari 125
    /* Allocate frames from zone buddy system */
126
    cur = buddy_system_alloc(zone->buddy_system, order);
1 jermar 127
 
533 bondari 128
    /* frame will be actually a first frame of the block */
129
    frame = list_get_instance(cur, frame_t, buddy_link);
130
 
131
    /* get frame address */
479 bondari 132
    v = FRAME2ADDR(zone, frame);
533 bondari 133
 
368 jermar 134
    if (flags & FRAME_KA)
135
        v = PA2KA(v);
136
 
137
    spinlock_unlock(&zone->lock);
138
    spinlock_unlock(&zone_head_lock);
413 jermar 139
    interrupts_restore(ipl);
368 jermar 140
    return v;
533 bondari 141
 
1 jermar 142
}
143
 
368 jermar 144
/** Free a frame.
145
 *
146
 * Find respective frame structrue for supplied addr.
147
 * Decrement frame reference count.
148
 * If it drops to zero, move the frame structure to free list.
149
 *
150
 * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE.
1 jermar 151
 */
152
void frame_free(__address addr)
153
{
413 jermar 154
    ipl_t ipl;
368 jermar 155
    link_t *cur;
156
    zone_t *z;
157
    zone_t *zone = NULL;
158
    frame_t *frame;
159
    ASSERT(addr % FRAME_SIZE == 0);
1 jermar 160
 
413 jermar 161
    ipl = interrupts_disable();
368 jermar 162
    spinlock_lock(&zone_head_lock);
1 jermar 163
 
368 jermar 164
    /*
165
     * First, find host frame zone for addr.
166
     */
167
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
168
        z = list_get_instance(cur, zone_t, link);
169
 
170
        spinlock_lock(&z->lock);
171
 
172
        if (IS_KA(addr))
173
            addr = KA2PA(addr);
174
 
175
        /*
176
         * Check if addr belongs to z.
177
         */
178
        if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
179
            zone = z;
180
            break;
1 jermar 181
        }
368 jermar 182
        spinlock_unlock(&z->lock);
1 jermar 183
    }
184
 
368 jermar 185
    ASSERT(zone != NULL);
186
 
479 bondari 187
    frame = ADDR2FRAME(zone, addr);
533 bondari 188
 
368 jermar 189
    ASSERT(frame->refcount);
190
 
191
    if (!--frame->refcount) {
533 bondari 192
        buddy_system_free(zone->buddy_system, &frame->buddy_link);
368 jermar 193
    }
194
 
195
    spinlock_unlock(&zone->lock);  
196
 
197
    spinlock_unlock(&zone_head_lock);
413 jermar 198
    interrupts_restore(ipl);
1 jermar 199
}
200
 
368 jermar 201
/** Mark frame region not free.
202
 *
203
 * Mark frame region not free.
204
 *
205
 * @param start First address.
206
 * @param stop Last address.
207
 */
533 bondari 208
void frame_region_not_free(__address base, size_t size)
1 jermar 209
{
533 bondari 210
    count_t index;
211
    index = zone_blacklist_count++;
212
    ASSERT(base % FRAME_SIZE == 0);
213
 
214
    if (size % FRAME_SIZE != 0) {
536 bondari 215
        size = ALIGN(size, FRAME_SIZE);
533 bondari 216
    }
217
    ASSERT(size % FRAME_SIZE == 0);
218
    ASSERT(zone_blacklist_count <= ZONE_BLACKLIST_SIZE);
219
    zone_blacklist[index].base = base;
220
    zone_blacklist[index].size = size;
1 jermar 221
}
367 jermar 222
 
368 jermar 223
 
367 jermar 224
/** Initialize zonekeeping
225
 *
226
 * Initialize zonekeeping.
227
 */
228
void zone_init(void)
229
{
230
    spinlock_initialize(&zone_head_lock);
231
    list_initialize(&zone_head);
232
}
233
 
533 bondari 234
 
235
void zone_create_in_region(__address base, size_t size) {
236
    int i;
237
    zone_t * z;
238
    __address s; size_t sz;
239
 
240
    ASSERT(base % FRAME_SIZE == 0);
241
    ASSERT(size % FRAME_SIZE == 0);
242
 
243
    if (!size) return;
244
 
245
    for (i = 0; i < zone_blacklist_count; i++) {
246
        if (zone_blacklist[i].base >= base && zone_blacklist[i].base < base + size) {
247
            s = base; sz = zone_blacklist[i].base - base;
248
            ASSERT(base != s || sz != size);
249
            zone_create_in_region(s, sz);
250
 
251
            s = zone_blacklist[i].base + zone_blacklist[i].size;
252
            sz = (base + size) - (zone_blacklist[i].base + zone_blacklist[i].size);
253
            ASSERT(base != s || sz != size);
254
            zone_create_in_region(s, sz);
255
            return;
256
 
257
        }
258
    }
259
 
260
    z = zone_create(base, size, 0);
261
 
262
    if (!z) {
263
        panic("Cannot allocate zone (%dB).\n", size);
264
    }
265
 
266
    zone_attach(z);
267
}
268
 
269
 
270
 
367 jermar 271
/** Create frame zone
272
 *
273
 * Create new frame zone.
274
 *
275
 * @param start Physical address of the first frame within the zone.
276
 * @param size Size of the zone. Must be a multiple of FRAME_SIZE.
277
 * @param flags Zone flags.
278
 *
279
 * @return Initialized zone.
280
 */
533 bondari 281
zone_t * zone_create(__address start, size_t size, int flags)
367 jermar 282
{
283
    zone_t *z;
284
    count_t cnt;
285
    int i;
479 bondari 286
    __u8 max_order;
533 bondari 287
 
288
    /* hack for bug #10 */
289
    // if (start == 0x100000) size -= (FRAME_SIZE * 256);
290
 
291
    // printf("ZONE_CREATE()   %X - %X (%d kbytes)          \n", start, start+size, size/1024); 
367 jermar 292
    ASSERT(start % FRAME_SIZE == 0);
293
    ASSERT(size % FRAME_SIZE == 0);
294
 
295
    cnt = size / FRAME_SIZE;
296
 
374 jermar 297
    z = (zone_t *) early_malloc(sizeof(zone_t));
367 jermar 298
    if (z) {
299
        link_initialize(&z->link);
300
        spinlock_initialize(&z->lock);
301
 
302
        z->base = start;
303
        z->flags = flags;
304
 
305
        z->free_count = cnt;
306
        list_initialize(&z->free_head);
307
 
308
        z->busy_count = 0;
309
 
374 jermar 310
        z->frames = (frame_t *) early_malloc(cnt * sizeof(frame_t));
367 jermar 311
        if (!z->frames) {
375 jermar 312
            early_free(z);
367 jermar 313
            return NULL;
314
        }
315
 
316
        for (i = 0; i<cnt; i++) {
317
            frame_initialize(&z->frames[i], z);
318
            list_append(&z->frames[i].link, &z->free_head);
319
        }
320
 
479 bondari 321
        /*
322
         * Create buddy system for the zone
323
         */
324
        for (max_order = 0; cnt >> max_order; max_order++);
489 jermar 325
        z->buddy_system = buddy_system_create(max_order, &zone_buddy_system_operations, (void *) z);
533 bondari 326
 
327
        /* Stuffing frames */
328
        for (i = 0; i<cnt; i++) {
329
            z->frames[i].refcount = 0;
330
            buddy_system_free(z->buddy_system, &z->frames[i].buddy_link);  
331
        }
367 jermar 332
    }
333
    return z;
334
}
335
 
336
/** Attach frame zone
337
 *
338
 * Attach frame zone to zone list.
339
 *
340
 * @param zone Zone to be attached.
341
 */
342
void zone_attach(zone_t *zone)
343
{
413 jermar 344
    ipl_t ipl;
367 jermar 345
 
413 jermar 346
    ipl = interrupts_disable();
367 jermar 347
    spinlock_lock(&zone_head_lock);
348
 
349
    list_append(&zone->link, &zone_head);
350
 
351
    spinlock_unlock(&zone_head_lock);
413 jermar 352
    interrupts_restore(ipl);
367 jermar 353
}
354
 
355
/** Initialize frame structure
356
 *
357
 * Initialize frame structure.
358
 *
359
 * @param frame Frame structure to be initialized.
360
 * @param zone Host frame zone.
361
 */
362
void frame_initialize(frame_t *frame, zone_t *zone)
363
{
533 bondari 364
    frame->refcount = 1;
365
    frame->buddy_order = 0;
367 jermar 366
    link_initialize(&frame->link);
367
}
479 bondari 368
 
369
 
370
/** Buddy system find_buddy implementation
489 jermar 371
 *
372
 * @param b Buddy system.
480 bondari 373
 * @param block Block for which buddy should be found
479 bondari 374
 *
480 bondari 375
 * @return Buddy for given block if found
479 bondari 376
 */
489 jermar 377
link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block) {
480 bondari 378
    frame_t * frame, * f;
379
    zone_t * zone;
380
    link_t * cur;
533 bondari 381
    count_t index;
480 bondari 382
    bool is_left, is_right;
479 bondari 383
 
480 bondari 384
    frame = list_get_instance(block, frame_t, buddy_link);
533 bondari 385
    zone = (zone_t *) b->data;
480 bondari 386
 
387
    /*
388
     * (FRAME_INDEX % 2^(ORDER+1)) == 0 ===> LEFT BUDDY
389
     * (FRAME_INDEX % 2^(ORDER+1)) == 2^(ORDER) ===> RIGHT BUDDY
390
     */
533 bondari 391
 
480 bondari 392
    is_left = IS_BUDDY_LEFT_BLOCK(zone, frame);
393
    is_right = IS_BUDDY_RIGHT_BLOCK(zone, frame);
394
 
395
    ASSERT((is_left || is_right) && (!is_left || !is_right));
396
 
533 bondari 397
    /*
398
     * test left buddy
399
     */
400
    if (is_left) {
401
        index = (FRAME_INDEX(zone, frame)) + (1 << frame->buddy_order);
402
    } else if (is_right) {
403
        index = (FRAME_INDEX(zone, frame)) - (1 << frame->buddy_order);
404
    }
405
 
406
    if (FRAME_INDEX_VALID(zone, index)) {
407
        if (    zone->frames[index].buddy_order == frame->buddy_order &&
408
            zone->frames[index].refcount == 0) {
409
            return &zone->frames[index].buddy_link;
480 bondari 410
        }
411
    }
412
 
413
    return NULL;
414
 
479 bondari 415
}
416
 
417
/** Buddy system bisect implementation
418
 *
489 jermar 419
 * @param b Buddy system.
480 bondari 420
 * @param block Block to bisect
421
 *
422
 * @return right block
479 bondari 423
 */
489 jermar 424
link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) {
480 bondari 425
    frame_t * frame_l, * frame_r;
426
    frame_l = list_get_instance(block, frame_t, buddy_link);
533 bondari 427
    frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
480 bondari 428
    return &frame_r->buddy_link;
479 bondari 429
}
430
 
431
/** Buddy system coalesce implementation
432
 *
489 jermar 433
 * @param b Buddy system.
480 bondari 434
 * @param block_1 First block
435
 * @param block_2 First block's buddy
436
 *
437
 * @return Coalesced block (actually block that represents lower address)
479 bondari 438
 */
489 jermar 439
link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1, link_t * block_2) {
480 bondari 440
    frame_t * frame1, * frame2;
441
    frame1 = list_get_instance(block_1, frame_t, buddy_link);
442
    frame2 = list_get_instance(block_2, frame_t, buddy_link);
533 bondari 443
    return frame1 < frame2 ? block_1 : block_2;
479 bondari 444
}
445
 
446
/** Buddy system set_order implementation
489 jermar 447
 *
448
 * @param b Buddy system.
480 bondari 449
 * @param block Buddy system block
450
 * @param order Order to set
479 bondari 451
 */
489 jermar 452
void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order) {
480 bondari 453
    frame_t * frame;
454
    frame = list_get_instance(block, frame_t, buddy_link);
455
    frame->buddy_order = order;
479 bondari 456
}
457
 
458
/** Buddy system get_order implementation
489 jermar 459
 *
460
 * @param b Buddy system.
480 bondari 461
 * @param block Buddy system block
479 bondari 462
 *
480 bondari 463
 * @return Order of block
479 bondari 464
 */
489 jermar 465
__u8 zone_buddy_get_order(buddy_system_t *b, link_t * block) {
480 bondari 466
    frame_t * frame;
467
    frame = list_get_instance(block, frame_t, buddy_link);
468
    return frame->buddy_order;
479 bondari 469
}
533 bondari 470
 
471
/** Buddy system mark_busy implementation
472
 *
473
 * @param b Buddy system
474
 * @param block Buddy system block
475
 *
476
 */
477
void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) {
478
    frame_t * frame;
479
    frame = list_get_instance(block, frame_t, buddy_link);
480
    frame->refcount = 1;
481
}