Subversion Repositories HelenOS-historic

Rev

Rev 689 | Rev 703 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 689 Rev 701
1
/*
1
/*
2
 * Copyright (C) 2001-2005 Jakub Jermar
2
 * Copyright (C) 2001-2005 Jakub Jermar
3
 * Copyright (C) 2005 Sergey Bondari
3
 * Copyright (C) 2005 Sergey Bondari
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
#include <typedefs.h>
30
#include <typedefs.h>
31
#include <arch/types.h>
31
#include <arch/types.h>
32
#include <mm/heap.h>
32
#include <mm/heap.h>
33
#include <mm/frame.h>
33
#include <mm/frame.h>
34
#include <mm/vm.h>
34
#include <mm/vm.h>
35
#include <panic.h>
35
#include <panic.h>
36
#include <debug.h>
36
#include <debug.h>
37
#include <list.h>
37
#include <list.h>
38
#include <synch/spinlock.h>
38
#include <synch/spinlock.h>
39
#include <arch/asm.h>
39
#include <arch/asm.h>
40
#include <arch.h>
40
#include <arch.h>
41
#include <print.h>
41
#include <print.h>
42
#include <align.h>
42
#include <align.h>
43
 
43
 
44
SPINLOCK_INITIALIZE(zone_head_lock);    /**< this lock protects zone_head list */
44
SPINLOCK_INITIALIZE(zone_head_lock);    /**< this lock protects zone_head list */
45
LIST_INITIALIZE(zone_head);             /**< list of all zones in the system */
45
LIST_INITIALIZE(zone_head);             /**< list of all zones in the system */
46
 
46
 
47
/** Blacklist containing non-available areas of memory.
47
/** Blacklist containing non-available areas of memory.
48
 *
48
 *
49
 * This blacklist is used to exclude frames that cannot be allocated
49
 * This blacklist is used to exclude frames that cannot be allocated
50
 * (e.g. kernel memory) from available memory map.
50
 * (e.g. kernel memory) from available memory map.
51
 */
51
 */
52
region_t zone_blacklist[ZONE_BLACKLIST_SIZE];
52
region_t zone_blacklist[ZONE_BLACKLIST_SIZE];
53
count_t zone_blacklist_count = 0;
53
count_t zone_blacklist_count = 0;
54
 
54
 
55
static struct buddy_system_operations  zone_buddy_system_operations = {
55
static struct buddy_system_operations  zone_buddy_system_operations = {
56
    .find_buddy = zone_buddy_find_buddy,
56
    .find_buddy = zone_buddy_find_buddy,
57
    .bisect = zone_buddy_bisect,
57
    .bisect = zone_buddy_bisect,
58
    .coalesce = zone_buddy_coalesce,
58
    .coalesce = zone_buddy_coalesce,
59
    .set_order = zone_buddy_set_order,
59
    .set_order = zone_buddy_set_order,
60
    .get_order = zone_buddy_get_order,
60
    .get_order = zone_buddy_get_order,
61
    .mark_busy = zone_buddy_mark_busy,
61
    .mark_busy = zone_buddy_mark_busy,
62
};
62
};
63
 
63
 
64
/** Initialize physical memory management
64
/** Initialize physical memory management
65
 *
65
 *
66
 * Initialize physical memory managemnt.
66
 * Initialize physical memory managemnt.
67
 */
67
 */
68
void frame_init(void)
68
void frame_init(void)
69
{
69
{
70
    if (config.cpu_active == 1) {
70
    if (config.cpu_active == 1) {
71
        frame_region_not_free(KA2PA(config.base), config.kernel_size);
71
        frame_region_not_free(KA2PA(config.base), config.kernel_size);
72
        if (config.init_size > 0)
72
        if (config.init_size > 0)
73
            frame_region_not_free(KA2PA(config.init_addr), config.init_size);
73
            frame_region_not_free(KA2PA(config.init_addr), config.init_size);
74
    }
74
    }
75
 
75
 
76
    frame_arch_init();
76
    frame_arch_init();
77
}
77
}
78
 
78
 
79
/** Allocate power-of-two frames of physical memory.
79
/** Allocate power-of-two frames of physical memory.
80
 *
80
 *
81
 * @param flags Flags for host zone selection and address processing.
81
 * @param flags Flags for host zone selection and address processing.
82
 * @param order Allocate exactly 2^order frames.
82
 * @param order Allocate exactly 2^order frames.
83
 *
83
 *
84
 * @return Allocated frame.
84
 * @return Allocated frame.
85
 */
85
 */
86
__address frame_alloc(int flags, __u8 order, int * status)
86
__address frame_alloc(int flags, __u8 order, int * status)
87
{
87
{
88
    ipl_t ipl;
88
    ipl_t ipl;
89
    link_t *cur, *tmp;
89
    link_t *cur, *tmp;
90
    zone_t *z;
90
    zone_t *z;
91
    zone_t *zone = NULL;
91
    zone_t *zone = NULL;
92
    frame_t *frame = NULL;
92
    frame_t *frame = NULL;
93
    __address v;
93
    __address v;
94
   
94
   
95
loop:
95
loop:
96
    ipl = interrupts_disable();
96
    ipl = interrupts_disable();
97
    spinlock_lock(&zone_head_lock);
97
    spinlock_lock(&zone_head_lock);
98
 
98
 
99
    /*
99
    /*
100
     * First, find suitable frame zone.
100
     * First, find suitable frame zone.
101
     */
101
     */
102
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
102
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
103
        z = list_get_instance(cur, zone_t, link);
103
        z = list_get_instance(cur, zone_t, link);
104
       
104
       
105
        spinlock_lock(&z->lock);
105
        spinlock_lock(&z->lock);
106
 
106
 
107
        /* Check if the zone has 2^order frames area available  */
107
        /* Check if the zone has 2^order frames area available  */
108
        if (buddy_system_can_alloc(z->buddy_system, order)) {
108
        if (buddy_system_can_alloc(z->buddy_system, order)) {
109
            zone = z;
109
            zone = z;
110
            break;
110
            break;
111
        }
111
        }
112
       
112
       
113
        spinlock_unlock(&z->lock);
113
        spinlock_unlock(&z->lock);
114
    }
114
    }
115
   
115
   
116
    if (!zone) {
116
    if (!zone) {
117
        if (flags & FRAME_PANIC)
117
        if (flags & FRAME_PANIC)
118
            panic("Can't allocate frame.\n");
118
            panic("Can't allocate frame.\n");
119
       
119
       
120
       
-
 
121
       
-
 
122
        /*
120
        /*
123
         * TODO: Sleep until frames are available again.
121
         * TODO: Sleep until frames are available again.
124
         */
122
         */
125
        spinlock_unlock(&zone_head_lock);
123
        spinlock_unlock(&zone_head_lock);
126
        interrupts_restore(ipl);
124
        interrupts_restore(ipl);
127
 
125
 
128
        if (flags & FRAME_NON_BLOCKING) {
126
        if (flags & FRAME_NON_BLOCKING) {
129
            ASSERT(status != NULL);
127
            ASSERT(status != NULL);
130
            *status = FRAME_NO_MEMORY;
128
            *status = FRAME_NO_MEMORY;
131
            return NULL;
129
            return NULL;
132
        }
130
        }
133
       
131
       
134
        panic("Sleep not implemented.\n");
132
        panic("Sleep not implemented.\n");
135
        goto loop;
133
        goto loop;
136
    }
134
    }
137
       
135
       
138
 
136
 
139
    /* Allocate frames from zone buddy system */
137
    /* Allocate frames from zone buddy system */
140
    tmp = buddy_system_alloc(zone->buddy_system, order);
138
    tmp = buddy_system_alloc(zone->buddy_system, order);
141
   
139
   
142
    ASSERT(tmp);
140
    ASSERT(tmp);
143
   
141
   
144
    /* Update zone information. */
142
    /* Update zone information. */
145
    zone->free_count -= (1 << order);
143
    zone->free_count -= (1 << order);
146
    zone->busy_count += (1 << order);
144
    zone->busy_count += (1 << order);
147
 
145
 
148
    /* Frame will be actually a first frame of the block. */
146
    /* Frame will be actually a first frame of the block. */
149
    frame = list_get_instance(tmp, frame_t, buddy_link);
147
    frame = list_get_instance(tmp, frame_t, buddy_link);
150
   
148
   
151
    /* get frame address */
149
    /* get frame address */
152
    v = FRAME2ADDR(zone, frame);
150
    v = FRAME2ADDR(zone, frame);
153
 
151
 
154
    spinlock_unlock(&zone->lock);
152
    spinlock_unlock(&zone->lock);
155
    spinlock_unlock(&zone_head_lock);
153
    spinlock_unlock(&zone_head_lock);
156
    interrupts_restore(ipl);
154
    interrupts_restore(ipl);
157
 
155
 
158
 
-
 
159
    if (flags & FRAME_KA)
156
    if (flags & FRAME_KA)
160
        v = PA2KA(v);
157
        v = PA2KA(v);
161
   
158
   
162
    if (flags & FRAME_NON_BLOCKING) {
159
    if (flags & FRAME_NON_BLOCKING) {
163
        ASSERT(status != NULL);
160
        ASSERT(status != NULL);
164
        *status = FRAME_OK;
161
        *status = FRAME_OK;
165
    }
162
    }
166
   
163
   
167
    return v;
164
    return v;
168
}
165
}
169
 
166
 
170
/** Free a frame.
167
/** Free a frame.
171
 *
168
 *
172
 * Find respective frame structrue for supplied addr.
169
 * Find respective frame structrue for supplied addr.
173
 * Decrement frame reference count.
170
 * Decrement frame reference count.
174
 * If it drops to zero, move the frame structure to free list.
171
 * If it drops to zero, move the frame structure to free list.
175
 *
172
 *
176
 * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE.
173
 * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE.
177
 */
174
 */
178
void frame_free(__address addr)
175
void frame_free(__address addr)
179
{
176
{
180
    ipl_t ipl;
177
    ipl_t ipl;
181
    link_t *cur;
178
    link_t *cur;
182
    zone_t *z;
179
    zone_t *z;
183
    zone_t *zone = NULL;
180
    zone_t *zone = NULL;
184
    frame_t *frame;
181
    frame_t *frame;
185
    ASSERT(addr % FRAME_SIZE == 0);
182
    ASSERT(addr % FRAME_SIZE == 0);
186
   
183
   
187
    ipl = interrupts_disable();
184
    ipl = interrupts_disable();
188
    spinlock_lock(&zone_head_lock);
185
    spinlock_lock(&zone_head_lock);
189
   
186
   
190
    /*
187
    /*
191
     * First, find host frame zone for addr.
188
     * First, find host frame zone for addr.
192
     */
189
     */
193
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
190
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
194
        z = list_get_instance(cur, zone_t, link);
191
        z = list_get_instance(cur, zone_t, link);
195
       
192
       
196
        spinlock_lock(&z->lock);
193
        spinlock_lock(&z->lock);
197
       
194
       
198
        if (IS_KA(addr))
195
        if (IS_KA(addr))
199
            addr = KA2PA(addr);
196
            addr = KA2PA(addr);
200
       
197
       
201
        /*
198
        /*
202
         * Check if addr belongs to z.
199
         * Check if addr belongs to z.
203
         */
200
         */
204
        if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
201
        if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
205
            zone = z;
202
            zone = z;
206
            break;
203
            break;
207
        }
204
        }
-
 
205
 
208
        spinlock_unlock(&z->lock);
206
        spinlock_unlock(&z->lock);
209
    }
207
    }
210
   
208
   
211
    ASSERT(zone != NULL);
209
    ASSERT(zone != NULL);
212
   
210
   
213
    frame = ADDR2FRAME(zone, addr);
211
    frame = ADDR2FRAME(zone, addr);
214
 
212
 
215
    ASSERT(frame->refcount);
213
    ASSERT(frame->refcount);
216
 
214
 
217
    if (!--frame->refcount) {
215
    if (!--frame->refcount) {
218
        buddy_system_free(zone->buddy_system, &frame->buddy_link);
216
        buddy_system_free(zone->buddy_system, &frame->buddy_link);
219
    }
217
    }
220
 
218
 
221
    /* Update zone information. */
219
    /* Update zone information. */
222
    zone->free_count += (1 << frame->buddy_order);
220
    zone->free_count += (1 << frame->buddy_order);
223
    zone->busy_count -= (1 << frame->buddy_order);
221
    zone->busy_count -= (1 << frame->buddy_order);
224
   
222
   
225
    spinlock_unlock(&zone->lock);
223
    spinlock_unlock(&zone->lock);
226
    spinlock_unlock(&zone_head_lock);
224
    spinlock_unlock(&zone_head_lock);
227
    interrupts_restore(ipl);
225
    interrupts_restore(ipl);
228
}
226
}
229
 
227
 
230
/** Mark frame region not free.
228
/** Mark frame region not free.
231
 *
229
 *
232
 * Mark frame region not free.
230
 * Mark frame region not free.
233
 *
231
 *
234
 * @param base Base address of non-available region.
232
 * @param base Base address of non-available region.
235
 * @param size Size of non-available region.
233
 * @param size Size of non-available region.
236
 */
234
 */
237
void frame_region_not_free(__address base, size_t size)
235
void frame_region_not_free(__address base, size_t size)
238
{
236
{
239
    index_t index;
237
    index_t index;
240
    index = zone_blacklist_count++;
238
    index = zone_blacklist_count++;
241
 
239
 
242
    /* Force base to the nearest lower address frame boundary. */
240
    /* Force base to the nearest lower address frame boundary. */
243
    base = ALIGN_DOWN(base, FRAME_SIZE);
241
    base = ALIGN_DOWN(base, FRAME_SIZE);
244
    /* Align size to frame boundary. */
242
    /* Align size to frame boundary. */
245
    size = ALIGN_UP(size, FRAME_SIZE);
243
    size = ALIGN_UP(size, FRAME_SIZE);
246
 
244
 
247
    ASSERT(index < ZONE_BLACKLIST_SIZE);
245
    ASSERT(index < ZONE_BLACKLIST_SIZE);
248
    zone_blacklist[index].base = base;
246
    zone_blacklist[index].base = base;
249
    zone_blacklist[index].size = size;
247
    zone_blacklist[index].size = size;
250
}
248
}
251
 
249
 
252
/** Create frame zones in region of available memory.
250
/** Create frame zones in region of available memory.
253
 *
251
 *
254
 * Avoid any black listed areas of non-available memory.
252
 * Avoid any black listed areas of non-available memory.
255
 * Assume that the black listed areas cannot overlap
253
 * Assume that the black listed areas cannot overlap
256
 * one another or cross available memory region boundaries.
254
 * one another or cross available memory region boundaries.
257
 *
255
 *
258
 * @param base Base address of available memory region.
256
 * @param base Base address of available memory region.
259
 * @param size Size of the region.
257
 * @param size Size of the region.
260
 */
258
 */
261
void zone_create_in_region(__address base, size_t size) {
259
void zone_create_in_region(__address base, size_t size) {
262
    int i;
260
    int i;
263
    zone_t * z;
261
    zone_t * z;
264
    __address s;
262
    __address s;
265
    size_t sz;
263
    size_t sz;
266
   
264
   
267
    ASSERT(base % FRAME_SIZE == 0);
265
    ASSERT(base % FRAME_SIZE == 0);
268
    ASSERT(size % FRAME_SIZE == 0);
266
    ASSERT(size % FRAME_SIZE == 0);
269
   
267
   
270
    if (!size)
268
    if (!size)
271
        return;
269
        return;
272
 
270
 
273
    for (i = 0; i < zone_blacklist_count; i++) {
271
    for (i = 0; i < zone_blacklist_count; i++) {
274
        if (zone_blacklist[i].base >= base && zone_blacklist[i].base < base + size) {
272
        if (zone_blacklist[i].base >= base && zone_blacklist[i].base < base + size) {
275
            s = base; sz = zone_blacklist[i].base - base;
273
            s = base; sz = zone_blacklist[i].base - base;
276
            ASSERT(base != s || sz != size);
274
            ASSERT(base != s || sz != size);
277
            zone_create_in_region(s, sz);
275
            zone_create_in_region(s, sz);
278
           
276
           
279
            s = zone_blacklist[i].base + zone_blacklist[i].size;
277
            s = zone_blacklist[i].base + zone_blacklist[i].size;
280
            sz = (base + size) - (zone_blacklist[i].base + zone_blacklist[i].size);
278
            sz = (base + size) - (zone_blacklist[i].base + zone_blacklist[i].size);
281
            ASSERT(base != s || sz != size);
279
            ASSERT(base != s || sz != size);
282
            zone_create_in_region(s, sz);
280
            zone_create_in_region(s, sz);
283
            return;
281
            return;
284
       
282
       
285
        }
283
        }
286
    }
284
    }
287
   
285
   
288
    z = zone_create(base, size, 0);
286
    z = zone_create(base, size, 0);
289
 
287
 
290
    if (!z) {
288
    if (!z) {
291
        panic("Cannot allocate zone (base=%P, size=%d).\n", base, size);
289
        panic("Cannot allocate zone (base=%P, size=%d).\n", base, size);
292
    }
290
    }
293
   
291
   
294
    zone_attach(z);
292
    zone_attach(z);
295
}
293
}
296
 
294
 
297
 
295
 
298
/** Create frame zone
296
/** Create frame zone
299
 *
297
 *
300
 * Create new frame zone.
298
 * Create new frame zone.
301
 *
299
 *
302
 * @param start Physical address of the first frame within the zone.
300
 * @param start Physical address of the first frame within the zone.
303
 * @param size Size of the zone. Must be a multiple of FRAME_SIZE.
301
 * @param size Size of the zone. Must be a multiple of FRAME_SIZE.
304
 * @param flags Zone flags.
302
 * @param flags Zone flags.
305
 *
303
 *
306
 * @return Initialized zone.
304
 * @return Initialized zone.
307
 */
305
 */
308
zone_t * zone_create(__address start, size_t size, int flags)
306
zone_t * zone_create(__address start, size_t size, int flags)
309
{
307
{
310
    zone_t *z;
308
    zone_t *z;
311
    count_t cnt;
309
    count_t cnt;
312
    int i;
310
    int i;
313
    __u8 max_order;
311
    __u8 max_order;
314
 
312
 
315
    ASSERT(start % FRAME_SIZE == 0);
313
    ASSERT(start % FRAME_SIZE == 0);
316
    ASSERT(size % FRAME_SIZE == 0);
314
    ASSERT(size % FRAME_SIZE == 0);
317
   
315
   
318
    cnt = size / FRAME_SIZE;
316
    cnt = size / FRAME_SIZE;
319
   
317
   
320
    z = (zone_t *) early_malloc(sizeof(zone_t));
318
    z = (zone_t *) early_malloc(sizeof(zone_t));
321
    if (z) {
319
    if (z) {
322
        link_initialize(&z->link);
320
        link_initialize(&z->link);
323
        spinlock_initialize(&z->lock, "zone_lock");
321
        spinlock_initialize(&z->lock, "zone_lock");
324
   
322
   
325
        z->base = start;
323
        z->base = start;
326
        z->flags = flags;
324
        z->flags = flags;
327
 
325
 
328
        z->free_count = cnt;
326
        z->free_count = cnt;
329
        z->busy_count = 0;
327
        z->busy_count = 0;
330
       
328
       
331
        z->frames = (frame_t *) early_malloc(cnt * sizeof(frame_t));
329
        z->frames = (frame_t *) early_malloc(cnt * sizeof(frame_t));
332
        if (!z->frames) {
330
        if (!z->frames) {
333
            early_free(z);
331
            early_free(z);
334
            return NULL;
332
            return NULL;
335
        }
333
        }
336
       
334
       
337
        for (i = 0; i<cnt; i++) {
335
        for (i = 0; i<cnt; i++) {
338
            frame_initialize(&z->frames[i], z);
336
            frame_initialize(&z->frames[i], z);
339
        }
337
        }
340
       
338
       
341
        /*
339
        /*
342
         * Create buddy system for the zone
340
         * Create buddy system for the zone
343
         */
341
         */
344
        for (max_order = 0; cnt >> max_order; max_order++)
342
        for (max_order = 0; cnt >> max_order; max_order++)
345
            ;
343
            ;
346
        z->buddy_system = buddy_system_create(max_order, &zone_buddy_system_operations, (void *) z);
344
        z->buddy_system = buddy_system_create(max_order, &zone_buddy_system_operations, (void *) z);
347
       
345
       
348
        /* Stuffing frames */
346
        /* Stuffing frames */
349
        for (i = 0; i<cnt; i++) {
347
        for (i = 0; i<cnt; i++) {
350
            z->frames[i].refcount = 0;
348
            z->frames[i].refcount = 0;
351
            buddy_system_free(z->buddy_system, &z->frames[i].buddy_link);  
349
            buddy_system_free(z->buddy_system, &z->frames[i].buddy_link);  
352
        }
350
        }
353
    }
351
    }
354
    return z;
352
    return z;
355
}
353
}
356
 
354
 
357
/** Attach frame zone
355
/** Attach frame zone
358
 *
356
 *
359
 * Attach frame zone to zone list.
357
 * Attach frame zone to zone list.
360
 *
358
 *
361
 * @param zone Zone to be attached.
359
 * @param zone Zone to be attached.
362
 */
360
 */
363
void zone_attach(zone_t *zone)
361
void zone_attach(zone_t *zone)
364
{
362
{
365
    ipl_t ipl;
363
    ipl_t ipl;
366
   
364
   
367
    ipl = interrupts_disable();
365
    ipl = interrupts_disable();
368
    spinlock_lock(&zone_head_lock);
366
    spinlock_lock(&zone_head_lock);
369
   
367
   
370
    list_append(&zone->link, &zone_head);
368
    list_append(&zone->link, &zone_head);
371
   
369
   
372
    spinlock_unlock(&zone_head_lock);
370
    spinlock_unlock(&zone_head_lock);
373
    interrupts_restore(ipl);
371
    interrupts_restore(ipl);
374
}
372
}
375
 
373
 
376
/** Initialize frame structure
374
/** Initialize frame structure
377
 *
375
 *
378
 * Initialize frame structure.
376
 * Initialize frame structure.
379
 *
377
 *
380
 * @param frame Frame structure to be initialized.
378
 * @param frame Frame structure to be initialized.
381
 * @param zone Host frame zone.
379
 * @param zone Host frame zone.
382
 */
380
 */
383
void frame_initialize(frame_t *frame, zone_t *zone)
381
void frame_initialize(frame_t *frame, zone_t *zone)
384
{
382
{
385
    frame->refcount = 1;
383
    frame->refcount = 1;
386
    frame->buddy_order = 0;
384
    frame->buddy_order = 0;
387
}
385
}
388
 
386
 
389
 
387
 
390
/** Buddy system find_buddy implementation
388
/** Buddy system find_buddy implementation
391
 *
389
 *
392
 * @param b Buddy system.
390
 * @param b Buddy system.
393
 * @param block Block for which buddy should be found
391
 * @param block Block for which buddy should be found
394
 *
392
 *
395
 * @return Buddy for given block if found
393
 * @return Buddy for given block if found
396
 */
394
 */
397
link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block) {
395
link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block) {
398
    frame_t * frame;
396
    frame_t * frame;
399
    zone_t * zone;
397
    zone_t * zone;
400
    index_t index;
398
    index_t index;
401
    bool is_left, is_right;
399
    bool is_left, is_right;
402
 
400
 
403
    frame = list_get_instance(block, frame_t, buddy_link);
401
    frame = list_get_instance(block, frame_t, buddy_link);
404
    zone = (zone_t *) b->data;
402
    zone = (zone_t *) b->data;
405
   
403
   
406
    ASSERT(IS_BUDDY_ORDER_OK(FRAME_INDEX(zone, frame), frame->buddy_order));
404
    ASSERT(IS_BUDDY_ORDER_OK(FRAME_INDEX(zone, frame), frame->buddy_order));
407
   
405
   
408
    is_left = IS_BUDDY_LEFT_BLOCK(zone, frame);
406
    is_left = IS_BUDDY_LEFT_BLOCK(zone, frame);
409
    is_right = IS_BUDDY_RIGHT_BLOCK(zone, frame);
407
    is_right = IS_BUDDY_RIGHT_BLOCK(zone, frame);
410
   
408
   
411
    ASSERT(is_left ^ is_right);
409
    ASSERT(is_left ^ is_right);
412
   
410
   
413
    if (is_left) {
411
    if (is_left) {
414
        index = (FRAME_INDEX(zone, frame)) + (1 << frame->buddy_order);
412
        index = (FRAME_INDEX(zone, frame)) + (1 << frame->buddy_order);
415
    } else { // if (is_right)
413
    } else { // if (is_right)
416
        index = (FRAME_INDEX(zone, frame)) - (1 << frame->buddy_order);
414
        index = (FRAME_INDEX(zone, frame)) - (1 << frame->buddy_order);
417
    }
415
    }
418
   
416
   
419
    if (FRAME_INDEX_VALID(zone, index)) {
417
    if (FRAME_INDEX_VALID(zone, index)) {
420
        if (    zone->frames[index].buddy_order == frame->buddy_order &&
418
        if (    zone->frames[index].buddy_order == frame->buddy_order &&
421
            zone->frames[index].refcount == 0) {
419
            zone->frames[index].refcount == 0) {
422
            return &zone->frames[index].buddy_link;
420
            return &zone->frames[index].buddy_link;
423
        }
421
        }
424
    }
422
    }
425
   
423
   
426
    return NULL;   
424
    return NULL;   
427
}
425
}
428
 
426
 
429
/** Buddy system bisect implementation
427
/** Buddy system bisect implementation
430
 *
428
 *
431
 * @param b Buddy system.
429
 * @param b Buddy system.
432
 * @param block Block to bisect
430
 * @param block Block to bisect
433
 *
431
 *
434
 * @return right block
432
 * @return right block
435
 */
433
 */
436
link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) {
434
link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) {
437
    frame_t * frame_l, * frame_r;
435
    frame_t * frame_l, * frame_r;
438
 
436
 
439
    frame_l = list_get_instance(block, frame_t, buddy_link);
437
    frame_l = list_get_instance(block, frame_t, buddy_link);
440
    frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
438
    frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
441
   
439
   
442
    return &frame_r->buddy_link;
440
    return &frame_r->buddy_link;
443
}
441
}
444
 
442
 
445
/** Buddy system coalesce implementation
443
/** Buddy system coalesce implementation
446
 *
444
 *
447
 * @param b Buddy system.
445
 * @param b Buddy system.
448
 * @param block_1 First block
446
 * @param block_1 First block
449
 * @param block_2 First block's buddy
447
 * @param block_2 First block's buddy
450
 *
448
 *
451
 * @return Coalesced block (actually block that represents lower address)
449
 * @return Coalesced block (actually block that represents lower address)
452
 */
450
 */
453
link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1, link_t * block_2) {
451
link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1, link_t * block_2) {
454
    frame_t * frame1, * frame2;
452
    frame_t * frame1, * frame2;
455
   
453
   
456
    frame1 = list_get_instance(block_1, frame_t, buddy_link);
454
    frame1 = list_get_instance(block_1, frame_t, buddy_link);
457
    frame2 = list_get_instance(block_2, frame_t, buddy_link);
455
    frame2 = list_get_instance(block_2, frame_t, buddy_link);
458
   
456
   
459
    return frame1 < frame2 ? block_1 : block_2;
457
    return frame1 < frame2 ? block_1 : block_2;
460
}
458
}
461
 
459
 
462
/** Buddy system set_order implementation
460
/** Buddy system set_order implementation
463
 *
461
 *
464
 * @param b Buddy system.
462
 * @param b Buddy system.
465
 * @param block Buddy system block
463
 * @param block Buddy system block
466
 * @param order Order to set
464
 * @param order Order to set
467
 */
465
 */
468
void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order) {
466
void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order) {
469
    frame_t * frame;
467
    frame_t * frame;
470
    frame = list_get_instance(block, frame_t, buddy_link);
468
    frame = list_get_instance(block, frame_t, buddy_link);
471
    frame->buddy_order = order;
469
    frame->buddy_order = order;
472
}
470
}
473
 
471
 
474
/** Buddy system get_order implementation
472
/** Buddy system get_order implementation
475
 *
473
 *
476
 * @param b Buddy system.
474
 * @param b Buddy system.
477
 * @param block Buddy system block
475
 * @param block Buddy system block
478
 *
476
 *
479
 * @return Order of block
477
 * @return Order of block
480
 */
478
 */
481
__u8 zone_buddy_get_order(buddy_system_t *b, link_t * block) {
479
__u8 zone_buddy_get_order(buddy_system_t *b, link_t * block) {
482
    frame_t * frame;
480
    frame_t * frame;
483
    frame = list_get_instance(block, frame_t, buddy_link);
481
    frame = list_get_instance(block, frame_t, buddy_link);
484
    return frame->buddy_order;
482
    return frame->buddy_order;
485
}
483
}
486
 
484
 
487
/** Buddy system mark_busy implementation
485
/** Buddy system mark_busy implementation
488
 *
486
 *
489
 * @param b Buddy system
487
 * @param b Buddy system
490
 * @param block Buddy system block
488
 * @param block Buddy system block
491
 *
489
 *
492
 */
490
 */
493
void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) {
491
void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) {
494
    frame_t * frame;
492
    frame_t * frame;
495
    frame = list_get_instance(block, frame_t, buddy_link);
493
    frame = list_get_instance(block, frame_t, buddy_link);
496
    frame->refcount = 1;
494
    frame->refcount = 1;
497
}
495
}
498
 
496
 
499
/** Prints list of zones
497
/** Prints list of zones
500
 *
498
 *
501
 */
499
 */
502
void zone_print_list(void) {
500
void zone_print_list(void) {
503
    zone_t *zone = NULL;
501
    zone_t *zone = NULL;
504
    link_t *cur;
502
    link_t *cur;
-
 
503
    ipl_t ipl;
-
 
504
 
-
 
505
    ipl = interrupts_disable();
505
    spinlock_lock(&zone_head_lock);
506
    spinlock_lock(&zone_head_lock);
506
    printf("Base address\tFree Frames\tBusy Frames\n");
507
    printf("Base address\tFree Frames\tBusy Frames\n");
507
    printf("------------\t-----------\t-----------\n");
508
    printf("------------\t-----------\t-----------\n");
508
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
509
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
509
        zone = list_get_instance(cur, zone_t, link);
510
        zone = list_get_instance(cur, zone_t, link);
510
        spinlock_lock(&zone->lock);
511
        spinlock_lock(&zone->lock);
511
        printf("%L\t%d\t\t%d\n",zone->base, zone->free_count, zone->busy_count);
512
        printf("%L\t%d\t\t%d\n",zone->base, zone->free_count, zone->busy_count);
-
 
513
        spinlock_unlock(&zone->lock);
512
    }
514
    }
513
    spinlock_unlock(&zone_head_lock);
515
    spinlock_unlock(&zone_head_lock);
514
 
-
 
-
 
516
    interrupts_restore(ipl);
515
}
517
}
516
 
518
 
517
/** Prints zone details
519
/** Prints zone details
518
 *
520
 *
519
 * @param base Zone base address
521
 * @param base Zone base address
520
 */
522
 */
521
void zone_print_one(__address base) {
523
void zone_print_one(__address base) {
522
    zone_t *zone = NULL, *z ;
524
    zone_t *zone = NULL, *z ;
523
    link_t *cur;
525
    link_t *cur;
524
   
526
    ipl_t ipl;
525
   
527
 
-
 
528
    ipl = interrupts_disable();
526
    spinlock_lock(&zone_head_lock);
529
    spinlock_lock(&zone_head_lock);
527
   
530
   
528
   
-
 
529
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
531
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
530
        z = list_get_instance(cur, zone_t, link);
532
        z = list_get_instance(cur, zone_t, link);
531
        if (base == z->base) {
533
        if (base == z->base) {
532
            zone = z;
534
            zone = z;
533
            break;
535
            break;
534
        }
536
        }
535
    }
537
    }
536
   
538
   
537
   
-
 
538
    if (!zone) {
539
    if (!zone) {
-
 
540
        spinlock_unlock(&zone_head_lock);
-
 
541
        interrupts_restore(ipl);
539
        printf("No zone with address %X\n", base);
542
        printf("No zone with address %X\n", base);
540
        return;
543
        return;
541
    }
544
    }
542
   
545
   
543
    spinlock_lock(&zone->lock);
546
    spinlock_lock(&zone->lock);
544
    printf("Memory zone information\n\n");
547
    printf("Memory zone information\n\n");
545
    printf("Zone base address: %P\n", zone->base);
548
    printf("Zone base address: %P\n", zone->base);
546
    printf("Zone size: %d frames (%d kbytes)\n", zone->free_count + zone->busy_count, ((zone->free_count + zone->busy_count) * FRAME_SIZE) >> 10);
549
    printf("Zone size: %d frames (%dK)\n", zone->free_count + zone->busy_count, ((zone->free_count + zone->busy_count) * FRAME_SIZE) >> 10);
547
    printf("Allocated space: %d frames (%d kbytes)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10);
550
    printf("Allocated space: %d frames (%dK)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10);
548
    printf("Available space: %d (%d kbytes)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10);
551
    printf("Available space: %d (%dK)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10);
549
   
552
   
550
    printf("\nBuddy allocator structures:\n\n");
553
    printf("\nBuddy allocator structures:\n\n");
551
    buddy_system_structure_print(zone->buddy_system, FRAME_SIZE);
554
    buddy_system_structure_print(zone->buddy_system, FRAME_SIZE);
552
   
555
   
553
    spinlock_unlock(&zone->lock);
556
    spinlock_unlock(&zone->lock);
554
    spinlock_unlock(&zone_head_lock);
557
    spinlock_unlock(&zone_head_lock);
555
   
-
 
-
 
558
    interrupts_restore(ipl);
556
}
559
}
557
 
560
 
558
 
561