Subversion Repositories HelenOS-historic

Rev

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

Rev 762 Rev 764
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/as.h>
34
#include <mm/as.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
/**
79
/**
80
 * Find AND LOCK zone that can allocate order frames
80
 * Find AND LOCK zone that can allocate order frames
81
 *
81
 *
82
 * Assume zone_head_lock is locked.
82
 * Assume zone_head_lock is locked.
83
 */
83
 */
84
static zone_t * find_free_zone(__u8 order)
84
static zone_t * find_free_zone(__u8 order)
85
{
85
{
86
    link_t *cur;
86
    link_t *cur;
87
    zone_t *z;
87
    zone_t *z;
88
 
88
 
89
    for (cur = zone_head.next; cur != &zone_head;cur = cur->next) {
89
    for (cur = zone_head.next; cur != &zone_head;cur = cur->next) {
90
        z = list_get_instance(cur, zone_t, link);
90
        z = list_get_instance(cur, zone_t, link);
91
       
91
       
92
        spinlock_lock(&z->lock);
92
        spinlock_lock(&z->lock);
93
       
93
       
94
        /* Check if the zone has 2^order frames area available  */
94
        /* Check if the zone has 2^order frames area available  */
95
        if (buddy_system_can_alloc(z->buddy_system, order))
95
        if (buddy_system_can_alloc(z->buddy_system, order))
96
            return z;
96
            return z;
97
       
97
       
98
        spinlock_unlock(&z->lock);
98
        spinlock_unlock(&z->lock);
99
    }
99
    }
100
    return NULL;
100
    return NULL;
101
}
101
}
102
 
102
 
103
/** Allocate power-of-two frames of physical memory.
103
/** Allocate power-of-two frames of physical memory.
104
 *
104
 *
105
 * @param flags Flags for host zone selection and address processing.
105
 * @param flags Flags for host zone selection and address processing.
106
 * @param order Allocate exactly 2^order frames.
106
 * @param order Allocate exactly 2^order frames.
107
 * @param pzone Pointer to preferred zone pointer, on output it changes
107
 * @param pzone Pointer to preferred zone pointer, on output it changes
108
 *              to the zone that the frame was really allocated to
108
 *              to the zone that the frame was really allocated to
109
 *
109
 *
110
 * @return Allocated frame.
110
 * @return Allocated frame.
111
 */
111
 */
112
__address frame_alloc(int flags, __u8 order, int * status, zone_t **pzone)
112
__address frame_alloc(int flags, __u8 order, int * status, zone_t **pzone)
113
{
113
{
114
    ipl_t ipl;
114
    ipl_t ipl;
115
    link_t *tmp;
115
    link_t *tmp;
116
    zone_t *zone = NULL;
116
    zone_t *zone = NULL;
117
    frame_t *frame = NULL;
117
    frame_t *frame = NULL;
118
    __address v;
118
    __address v;
119
   
119
   
120
loop:
120
loop:
121
    ipl = interrupts_disable();
121
    ipl = interrupts_disable();
122
    spinlock_lock(&zone_head_lock);
122
    spinlock_lock(&zone_head_lock);
123
 
123
 
124
    /*
124
    /*
125
     * First, find suitable frame zone.
125
     * First, find suitable frame zone.
126
     */
126
     */
127
    if (pzone && *pzone) {
127
    if (pzone && *pzone) {
128
        spinlock_lock(&(*pzone)->lock);
128
        spinlock_lock(&(*pzone)->lock);
129
        if (!buddy_system_can_alloc((*pzone)->buddy_system, order))
129
        if (!buddy_system_can_alloc((*pzone)->buddy_system, order))
130
            spinlock_unlock(&(*pzone)->lock);
130
            spinlock_unlock(&(*pzone)->lock);
131
        else
131
        else
132
            zone = *pzone;
132
            zone = *pzone;
133
    }
133
    }
134
    if (!zone) {
134
    if (!zone) {
135
        zone = find_free_zone(order);
135
        zone = find_free_zone(order);
136
        /* If no memory, reclaim some slab memory,
136
        /* If no memory, reclaim some slab memory,
137
           if it does not help, reclaim all */
137
           if it does not help, reclaim all */
138
        if (!zone && !(flags & FRAME_NO_RECLAIM))
138
        if (!zone && !(flags & FRAME_NO_RECLAIM))
139
            if (slab_reclaim(0) || slab_reclaim(SLAB_RECLAIM_ALL))
139
            if (slab_reclaim(0) || slab_reclaim(SLAB_RECLAIM_ALL))
140
                zone = find_free_zone(order);
140
                zone = find_free_zone(order);
141
    }
141
    }
142
 
142
 
143
    if (!zone) {
143
    if (!zone) {
144
        if (flags & FRAME_PANIC)
144
        if (flags & FRAME_PANIC)
145
            panic("Can't allocate frame.\n");
145
            panic("Can't allocate frame.\n");
146
       
146
       
147
        /*
147
        /*
148
         * TODO: Sleep until frames are available again.
148
         * TODO: Sleep until frames are available again.
149
         */
149
         */
150
        spinlock_unlock(&zone_head_lock);
150
        spinlock_unlock(&zone_head_lock);
151
        interrupts_restore(ipl);
151
        interrupts_restore(ipl);
152
 
152
 
153
        if (flags & FRAME_ATOMIC) {
153
        if (flags & FRAME_ATOMIC) {
154
            ASSERT(status != NULL);
154
            ASSERT(status != NULL);
155
            *status = FRAME_NO_MEMORY;
155
            *status = FRAME_NO_MEMORY;
156
            return NULL;
156
            return NULL;
157
        }
157
        }
158
       
158
       
159
        panic("Sleep not implemented.\n");
159
        panic("Sleep not implemented.\n");
160
        goto loop;
160
        goto loop;
161
    }
161
    }
162
       
162
       
163
    /* Allocate frames from zone buddy system */
163
    /* Allocate frames from zone buddy system */
164
    tmp = buddy_system_alloc(zone->buddy_system, order);
164
    tmp = buddy_system_alloc(zone->buddy_system, order);
165
   
165
   
166
    ASSERT(tmp);
166
    ASSERT(tmp);
167
   
167
   
168
    /* Update zone information. */
168
    /* Update zone information. */
169
    zone->free_count -= (1 << order);
169
    zone->free_count -= (1 << order);
170
    zone->busy_count += (1 << order);
170
    zone->busy_count += (1 << order);
171
 
171
 
172
    /* Frame will be actually a first frame of the block. */
172
    /* Frame will be actually a first frame of the block. */
173
    frame = list_get_instance(tmp, frame_t, buddy_link);
173
    frame = list_get_instance(tmp, frame_t, buddy_link);
174
   
174
   
175
    /* get frame address */
175
    /* get frame address */
176
    v = FRAME2ADDR(zone, frame);
176
    v = FRAME2ADDR(zone, frame);
177
 
177
 
178
    spinlock_unlock(&zone->lock);
178
    spinlock_unlock(&zone->lock);
179
    spinlock_unlock(&zone_head_lock);
179
    spinlock_unlock(&zone_head_lock);
180
    interrupts_restore(ipl);
180
    interrupts_restore(ipl);
181
 
181
 
182
    ASSERT(v == ALIGN_UP(v, FRAME_SIZE << order));
182
    ASSERT(v == ALIGN_UP(v, FRAME_SIZE << order));
183
 
183
 
184
    if (flags & FRAME_KA)
184
    if (flags & FRAME_KA)
185
        v = PA2KA(v);
185
        v = PA2KA(v);
186
   
186
   
187
    if (flags & FRAME_ATOMIC) {
-
 
188
        ASSERT(status != NULL);
187
    if (status)
189
        *status = FRAME_OK;
188
        *status = FRAME_OK;
190
    }
189
 
191
    if (pzone)
190
    if (pzone)
192
        *pzone = zone;
191
        *pzone = zone;
193
    return v;
192
    return v;
194
}
193
}
195
 
194
 
196
/** Convert address to zone pointer
195
/** Convert address to zone pointer
197
 *
196
 *
198
 * Assume zone_head_lock is held
197
 * Assume zone_head_lock is held
199
 *
198
 *
200
 * @param addr Physical address
199
 * @param addr Physical address
201
 * @param lock If true, lock the zone
200
 * @param lock If true, lock the zone
202
 */
201
 */
203
static zone_t * addr2zone(__address addr, int lock)
202
static zone_t * addr2zone(__address addr, int lock)
204
{
203
{
205
    link_t *cur;
204
    link_t *cur;
206
    zone_t *z = NULL;
205
    zone_t *z = NULL;
207
 
206
 
208
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
207
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
209
        z = list_get_instance(cur, zone_t, link);
208
        z = list_get_instance(cur, zone_t, link);
210
       
209
       
211
        spinlock_lock(&z->lock);
210
        spinlock_lock(&z->lock);
212
       
211
       
213
        /*
212
        /*
214
         * Check if addr belongs to z.
213
         * Check if addr belongs to z.
215
         */
214
         */
216
        if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
215
        if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
217
            if (!lock)
216
            if (!lock)
218
                spinlock_unlock(&z->lock);
217
                spinlock_unlock(&z->lock);
219
            return z;
218
            return z;
220
        }
219
        }
221
 
220
 
222
        spinlock_unlock(&z->lock);
221
        spinlock_unlock(&z->lock);
223
    }
222
    }
224
 
223
 
225
    panic("Cannot find addr2zone: 0x%X", addr);
224
    panic("Cannot find addr2zone: 0x%X", addr);
226
}
225
}
227
 
226
 
228
/** Return frame_t structure corresponding to address
227
/** Return frame_t structure corresponding to address
229
 *
228
 *
230
 *
229
 *
231
 */
230
 */
232
frame_t * frame_addr2frame(__address addr)
231
frame_t * frame_addr2frame(__address addr)
233
{
232
{
234
    ipl_t ipl;
233
    ipl_t ipl;
235
    frame_t *frame;
234
    frame_t *frame;
236
    zone_t *zone;
235
    zone_t *zone;
237
 
236
 
238
    if (IS_KA(addr))
237
    if (IS_KA(addr))
239
        addr = KA2PA(addr);
238
        addr = KA2PA(addr);
240
 
239
 
241
    /* Disable interrupts to avoid deadlocks with interrupt handlers */
240
    /* Disable interrupts to avoid deadlocks with interrupt handlers */
242
    ipl = interrupts_disable();
241
    ipl = interrupts_disable();
243
    spinlock_lock(&zone_head_lock);
242
    spinlock_lock(&zone_head_lock);
244
   
243
   
245
    zone = addr2zone(addr,0);
244
    zone = addr2zone(addr,0);
246
    frame = ADDR2FRAME(zone, addr);
245
    frame = ADDR2FRAME(zone, addr);
247
 
246
 
248
    spinlock_unlock(&zone_head_lock);
247
    spinlock_unlock(&zone_head_lock);
249
    interrupts_restore(ipl);
248
    interrupts_restore(ipl);
250
 
249
 
251
    return frame;
250
    return frame;
252
}
251
}
253
 
252
 
254
 
253
 
255
/** Free a frame.
254
/** Free a frame.
256
 *
255
 *
257
 * Find respective frame structrue for supplied addr.
256
 * Find respective frame structrue for supplied addr.
258
 * Decrement frame reference count.
257
 * Decrement frame reference count.
259
 * If it drops to zero, move the frame structure to free list.
258
 * If it drops to zero, move the frame structure to free list.
260
 *
259
 *
261
 * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE.
260
 * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE.
262
 */
261
 */
263
void frame_free(__address addr)
262
void frame_free(__address addr)
264
{
263
{
265
    ipl_t ipl;
264
    ipl_t ipl;
266
    zone_t *zone;
265
    zone_t *zone;
267
    frame_t *frame;
266
    frame_t *frame;
268
    int order;
267
    int order;
269
   
268
   
270
    ASSERT(addr % FRAME_SIZE == 0);
269
    ASSERT(addr % FRAME_SIZE == 0);
271
   
270
   
272
    if (IS_KA(addr))
271
    if (IS_KA(addr))
273
        addr = KA2PA(addr);
272
        addr = KA2PA(addr);
274
 
273
 
275
    ipl = interrupts_disable();
274
    ipl = interrupts_disable();
276
    spinlock_lock(&zone_head_lock);
275
    spinlock_lock(&zone_head_lock);
277
   
276
   
278
    /*
277
    /*
279
     * First, find host frame zone for addr.
278
     * First, find host frame zone for addr.
280
     */
279
     */
281
    zone = addr2zone(addr, 1); /* This locks the zone automatically */
280
    zone = addr2zone(addr, 1); /* This locks the zone automatically */
282
   
281
   
283
    frame = ADDR2FRAME(zone, addr);
282
    frame = ADDR2FRAME(zone, addr);
284
   
283
   
285
    /* remember frame order */
284
    /* remember frame order */
286
    order = frame->buddy_order;
285
    order = frame->buddy_order;
287
 
286
 
288
    ASSERT(frame->refcount);
287
    ASSERT(frame->refcount);
289
 
288
 
290
    if (!--frame->refcount) {
289
    if (!--frame->refcount) {
291
        buddy_system_free(zone->buddy_system, &frame->buddy_link);
290
        buddy_system_free(zone->buddy_system, &frame->buddy_link);
292
    }
291
    }
293
 
292
 
294
    /* Update zone information. */
293
    /* Update zone information. */
295
    zone->free_count += (1 << order);
294
    zone->free_count += (1 << order);
296
    zone->busy_count -= (1 << order);
295
    zone->busy_count -= (1 << order);
297
   
296
   
298
    spinlock_unlock(&zone->lock);
297
    spinlock_unlock(&zone->lock);
299
    spinlock_unlock(&zone_head_lock);
298
    spinlock_unlock(&zone_head_lock);
300
    interrupts_restore(ipl);
299
    interrupts_restore(ipl);
301
}
300
}
302
 
301
 
303
/** Mark frame region not free.
302
/** Mark frame region not free.
304
 *
303
 *
305
 * Mark frame region not free.
304
 * Mark frame region not free.
306
 *
305
 *
307
 * @param base Base address of non-available region.
306
 * @param base Base address of non-available region.
308
 * @param size Size of non-available region.
307
 * @param size Size of non-available region.
309
 */
308
 */
310
void frame_region_not_free(__address base, size_t size)
309
void frame_region_not_free(__address base, size_t size)
311
{
310
{
312
    index_t index;
311
    index_t index;
313
    index = zone_blacklist_count++;
312
    index = zone_blacklist_count++;
314
 
313
 
315
    /* Force base to the nearest lower address frame boundary. */
314
    /* Force base to the nearest lower address frame boundary. */
316
    base = ALIGN_DOWN(base, FRAME_SIZE);
315
    base = ALIGN_DOWN(base, FRAME_SIZE);
317
    /* Align size to frame boundary. */
316
    /* Align size to frame boundary. */
318
    size = ALIGN_UP(size, FRAME_SIZE);
317
    size = ALIGN_UP(size, FRAME_SIZE);
319
 
318
 
320
    ASSERT(index < ZONE_BLACKLIST_SIZE);
319
    ASSERT(index < ZONE_BLACKLIST_SIZE);
321
    zone_blacklist[index].base = base;
320
    zone_blacklist[index].base = base;
322
    zone_blacklist[index].size = size;
321
    zone_blacklist[index].size = size;
323
}
322
}
324
 
323
 
325
/** Create frame zones in region of available memory.
324
/** Create frame zones in region of available memory.
326
 *
325
 *
327
 * Avoid any black listed areas of non-available memory.
326
 * Avoid any black listed areas of non-available memory.
328
 * Assume that the black listed areas cannot overlap
327
 * Assume that the black listed areas cannot overlap
329
 * one another or cross available memory region boundaries.
328
 * one another or cross available memory region boundaries.
330
 *
329
 *
331
 * @param base Base address of available memory region.
330
 * @param base Base address of available memory region.
332
 * @param size Size of the region.
331
 * @param size Size of the region.
333
 */
332
 */
334
void zone_create_in_region(__address base, size_t size) {
333
void zone_create_in_region(__address base, size_t size) {
335
    int i;
334
    int i;
336
    zone_t * z;
335
    zone_t * z;
337
    __address s;
336
    __address s;
338
    size_t sz;
337
    size_t sz;
339
   
338
   
340
    ASSERT(base % FRAME_SIZE == 0);
339
    ASSERT(base % FRAME_SIZE == 0);
341
    ASSERT(size % FRAME_SIZE == 0);
340
    ASSERT(size % FRAME_SIZE == 0);
342
   
341
   
343
    if (!size)
342
    if (!size)
344
        return;
343
        return;
345
 
344
 
346
    for (i = 0; i < zone_blacklist_count; i++) {
345
    for (i = 0; i < zone_blacklist_count; i++) {
347
        if (zone_blacklist[i].base >= base && zone_blacklist[i].base < base + size) {
346
        if (zone_blacklist[i].base >= base && zone_blacklist[i].base < base + size) {
348
            s = base; sz = zone_blacklist[i].base - base;
347
            s = base; sz = zone_blacklist[i].base - base;
349
            ASSERT(base != s || sz != size);
348
            ASSERT(base != s || sz != size);
350
            zone_create_in_region(s, sz);
349
            zone_create_in_region(s, sz);
351
           
350
           
352
            s = zone_blacklist[i].base + zone_blacklist[i].size;
351
            s = zone_blacklist[i].base + zone_blacklist[i].size;
353
            sz = (base + size) - (zone_blacklist[i].base + zone_blacklist[i].size);
352
            sz = (base + size) - (zone_blacklist[i].base + zone_blacklist[i].size);
354
            ASSERT(base != s || sz != size);
353
            ASSERT(base != s || sz != size);
355
            zone_create_in_region(s, sz);
354
            zone_create_in_region(s, sz);
356
            return;
355
            return;
357
       
356
       
358
        }
357
        }
359
    }
358
    }
360
   
359
   
361
    z = zone_create(base, size, 0);
360
    z = zone_create(base, size, 0);
362
 
361
 
363
    if (!z) {
362
    if (!z) {
364
        panic("Cannot allocate zone (base=%P, size=%d).\n", base, size);
363
        panic("Cannot allocate zone (base=%P, size=%d).\n", base, size);
365
    }
364
    }
366
   
365
   
367
    zone_attach(z);
366
    zone_attach(z);
368
}
367
}
369
 
368
 
370
 
369
 
371
/** Create frame zone
370
/** Create frame zone
372
 *
371
 *
373
 * Create new frame zone.
372
 * Create new frame zone.
374
 *
373
 *
375
 * @param start Physical address of the first frame within the zone.
374
 * @param start Physical address of the first frame within the zone.
376
 * @param size Size of the zone. Must be a multiple of FRAME_SIZE.
375
 * @param size Size of the zone. Must be a multiple of FRAME_SIZE.
377
 * @param flags Zone flags.
376
 * @param flags Zone flags.
378
 *
377
 *
379
 * @return Initialized zone.
378
 * @return Initialized zone.
380
 */
379
 */
381
zone_t * zone_create(__address start, size_t size, int flags)
380
zone_t * zone_create(__address start, size_t size, int flags)
382
{
381
{
383
    zone_t *z;
382
    zone_t *z;
384
    count_t cnt;
383
    count_t cnt;
385
    int i;
384
    int i;
386
    __u8 max_order;
385
    __u8 max_order;
387
 
386
 
388
    ASSERT(start % FRAME_SIZE == 0);
387
    ASSERT(start % FRAME_SIZE == 0);
389
    ASSERT(size % FRAME_SIZE == 0);
388
    ASSERT(size % FRAME_SIZE == 0);
390
   
389
   
391
    cnt = size / FRAME_SIZE;
390
    cnt = size / FRAME_SIZE;
392
   
391
   
393
    z = (zone_t *) early_malloc(sizeof(zone_t));
392
    z = (zone_t *) early_malloc(sizeof(zone_t));
394
    if (z) {
393
    if (z) {
395
        link_initialize(&z->link);
394
        link_initialize(&z->link);
396
        spinlock_initialize(&z->lock, "zone_lock");
395
        spinlock_initialize(&z->lock, "zone_lock");
397
   
396
   
398
        z->base = start;
397
        z->base = start;
399
        z->base_index = start / FRAME_SIZE;
398
        z->base_index = start / FRAME_SIZE;
400
       
399
       
401
        z->flags = flags;
400
        z->flags = flags;
402
 
401
 
403
        z->free_count = cnt;
402
        z->free_count = cnt;
404
        z->busy_count = 0;
403
        z->busy_count = 0;
405
       
404
       
406
        z->frames = (frame_t *) early_malloc(cnt * sizeof(frame_t));
405
        z->frames = (frame_t *) early_malloc(cnt * sizeof(frame_t));
407
        if (!z->frames) {
406
        if (!z->frames) {
408
            early_free(z);
407
            early_free(z);
409
            return NULL;
408
            return NULL;
410
        }
409
        }
411
       
410
       
412
        for (i = 0; i<cnt; i++) {
411
        for (i = 0; i<cnt; i++) {
413
            frame_initialize(&z->frames[i], z);
412
            frame_initialize(&z->frames[i], z);
414
        }
413
        }
415
       
414
       
416
        /*
415
        /*
417
         * Create buddy system for the zone
416
         * Create buddy system for the zone
418
         */
417
         */
419
        for (max_order = 0; cnt >> max_order; max_order++)
418
        for (max_order = 0; cnt >> max_order; max_order++)
420
            ;
419
            ;
421
        z->buddy_system = buddy_system_create(max_order, &zone_buddy_system_operations, (void *) z);
420
        z->buddy_system = buddy_system_create(max_order, &zone_buddy_system_operations, (void *) z);
422
       
421
       
423
        /* Stuffing frames */
422
        /* Stuffing frames */
424
        for (i = 0; i<cnt; i++) {
423
        for (i = 0; i<cnt; i++) {
425
            z->frames[i].refcount = 0;
424
            z->frames[i].refcount = 0;
426
            buddy_system_free(z->buddy_system, &z->frames[i].buddy_link);  
425
            buddy_system_free(z->buddy_system, &z->frames[i].buddy_link);  
427
        }
426
        }
428
       
427
       
429
    }
428
    }
430
    return z;
429
    return z;
431
}
430
}
432
 
431
 
433
/** Attach frame zone
432
/** Attach frame zone
434
 *
433
 *
435
 * Attach frame zone to zone list.
434
 * Attach frame zone to zone list.
436
 *
435
 *
437
 * @param zone Zone to be attached.
436
 * @param zone Zone to be attached.
438
 */
437
 */
439
void zone_attach(zone_t *zone)
438
void zone_attach(zone_t *zone)
440
{
439
{
441
    ipl_t ipl;
440
    ipl_t ipl;
442
   
441
   
443
    ipl = interrupts_disable();
442
    ipl = interrupts_disable();
444
    spinlock_lock(&zone_head_lock);
443
    spinlock_lock(&zone_head_lock);
445
   
444
   
446
    list_append(&zone->link, &zone_head);
445
    list_append(&zone->link, &zone_head);
447
   
446
   
448
    spinlock_unlock(&zone_head_lock);
447
    spinlock_unlock(&zone_head_lock);
449
    interrupts_restore(ipl);
448
    interrupts_restore(ipl);
450
}
449
}
451
 
450
 
452
/** Initialize frame structure
451
/** Initialize frame structure
453
 *
452
 *
454
 * Initialize frame structure.
453
 * Initialize frame structure.
455
 *
454
 *
456
 * @param frame Frame structure to be initialized.
455
 * @param frame Frame structure to be initialized.
457
 * @param zone Host frame zone.
456
 * @param zone Host frame zone.
458
 */
457
 */
459
void frame_initialize(frame_t *frame, zone_t *zone)
458
void frame_initialize(frame_t *frame, zone_t *zone)
460
{
459
{
461
    frame->refcount = 1;
460
    frame->refcount = 1;
462
    frame->buddy_order = 0;
461
    frame->buddy_order = 0;
463
}
462
}
464
 
463
 
465
 
464
 
466
/** Buddy system find_buddy implementation
465
/** Buddy system find_buddy implementation
467
 *
466
 *
468
 * @param b Buddy system.
467
 * @param b Buddy system.
469
 * @param block Block for which buddy should be found
468
 * @param block Block for which buddy should be found
470
 *
469
 *
471
 * @return Buddy for given block if found
470
 * @return Buddy for given block if found
472
 */
471
 */
473
link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block) {
472
link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block) {
474
    frame_t * frame;
473
    frame_t * frame;
475
    zone_t * zone;
474
    zone_t * zone;
476
    index_t index;
475
    index_t index;
477
    bool is_left, is_right;
476
    bool is_left, is_right;
478
 
477
 
479
    frame = list_get_instance(block, frame_t, buddy_link);
478
    frame = list_get_instance(block, frame_t, buddy_link);
480
    zone = (zone_t *) b->data;
479
    zone = (zone_t *) b->data;
481
    ASSERT(IS_BUDDY_ORDER_OK(FRAME_INDEX_ABS(zone, frame), frame->buddy_order));
480
    ASSERT(IS_BUDDY_ORDER_OK(FRAME_INDEX_ABS(zone, frame), frame->buddy_order));
482
   
481
   
483
   
482
   
484
    is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame);
483
    is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame);
485
    is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame);
484
    is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame);
486
   
485
   
487
    ASSERT(is_left ^ is_right);
486
    ASSERT(is_left ^ is_right);
488
   
487
   
489
    if (is_left) {
488
    if (is_left) {
490
        index = (FRAME_INDEX(zone, frame)) + (1 << frame->buddy_order);
489
        index = (FRAME_INDEX(zone, frame)) + (1 << frame->buddy_order);
491
    } else { // if (is_right)
490
    } else { // if (is_right)
492
        index = (FRAME_INDEX(zone, frame)) - (1 << frame->buddy_order);
491
        index = (FRAME_INDEX(zone, frame)) - (1 << frame->buddy_order);
493
    }
492
    }
494
   
493
   
495
    if (FRAME_INDEX_VALID(zone, index)) {
494
    if (FRAME_INDEX_VALID(zone, index)) {
496
        if (    zone->frames[index].buddy_order == frame->buddy_order &&
495
        if (    zone->frames[index].buddy_order == frame->buddy_order &&
497
            zone->frames[index].refcount == 0) {
496
            zone->frames[index].refcount == 0) {
498
            return &zone->frames[index].buddy_link;
497
            return &zone->frames[index].buddy_link;
499
        }
498
        }
500
    }
499
    }
501
   
500
   
502
    return NULL;   
501
    return NULL;   
503
}
502
}
504
 
503
 
505
/** Buddy system bisect implementation
504
/** Buddy system bisect implementation
506
 *
505
 *
507
 * @param b Buddy system.
506
 * @param b Buddy system.
508
 * @param block Block to bisect
507
 * @param block Block to bisect
509
 *
508
 *
510
 * @return right block
509
 * @return right block
511
 */
510
 */
512
link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) {
511
link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) {
513
    frame_t * frame_l, * frame_r;
512
    frame_t * frame_l, * frame_r;
514
 
513
 
515
    frame_l = list_get_instance(block, frame_t, buddy_link);
514
    frame_l = list_get_instance(block, frame_t, buddy_link);
516
    frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
515
    frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
517
   
516
   
518
    return &frame_r->buddy_link;
517
    return &frame_r->buddy_link;
519
}
518
}
520
 
519
 
521
/** Buddy system coalesce implementation
520
/** Buddy system coalesce implementation
522
 *
521
 *
523
 * @param b Buddy system.
522
 * @param b Buddy system.
524
 * @param block_1 First block
523
 * @param block_1 First block
525
 * @param block_2 First block's buddy
524
 * @param block_2 First block's buddy
526
 *
525
 *
527
 * @return Coalesced block (actually block that represents lower address)
526
 * @return Coalesced block (actually block that represents lower address)
528
 */
527
 */
529
link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1, link_t * block_2) {
528
link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1, link_t * block_2) {
530
    frame_t * frame1, * frame2;
529
    frame_t * frame1, * frame2;
531
   
530
   
532
    frame1 = list_get_instance(block_1, frame_t, buddy_link);
531
    frame1 = list_get_instance(block_1, frame_t, buddy_link);
533
    frame2 = list_get_instance(block_2, frame_t, buddy_link);
532
    frame2 = list_get_instance(block_2, frame_t, buddy_link);
534
   
533
   
535
    return frame1 < frame2 ? block_1 : block_2;
534
    return frame1 < frame2 ? block_1 : block_2;
536
}
535
}
537
 
536
 
538
/** Buddy system set_order implementation
537
/** Buddy system set_order implementation
539
 *
538
 *
540
 * @param b Buddy system.
539
 * @param b Buddy system.
541
 * @param block Buddy system block
540
 * @param block Buddy system block
542
 * @param order Order to set
541
 * @param order Order to set
543
 */
542
 */
544
void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order) {
543
void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order) {
545
    frame_t * frame;
544
    frame_t * frame;
546
    frame = list_get_instance(block, frame_t, buddy_link);
545
    frame = list_get_instance(block, frame_t, buddy_link);
547
    frame->buddy_order = order;
546
    frame->buddy_order = order;
548
}
547
}
549
 
548
 
550
/** Buddy system get_order implementation
549
/** Buddy system get_order implementation
551
 *
550
 *
552
 * @param b Buddy system.
551
 * @param b Buddy system.
553
 * @param block Buddy system block
552
 * @param block Buddy system block
554
 *
553
 *
555
 * @return Order of block
554
 * @return Order of block
556
 */
555
 */
557
__u8 zone_buddy_get_order(buddy_system_t *b, link_t * block) {
556
__u8 zone_buddy_get_order(buddy_system_t *b, link_t * block) {
558
    frame_t * frame;
557
    frame_t * frame;
559
    frame = list_get_instance(block, frame_t, buddy_link);
558
    frame = list_get_instance(block, frame_t, buddy_link);
560
    return frame->buddy_order;
559
    return frame->buddy_order;
561
}
560
}
562
 
561
 
563
/** Buddy system mark_busy implementation
562
/** Buddy system mark_busy implementation
564
 *
563
 *
565
 * @param b Buddy system
564
 * @param b Buddy system
566
 * @param block Buddy system block
565
 * @param block Buddy system block
567
 *
566
 *
568
 */
567
 */
569
void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) {
568
void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) {
570
    frame_t * frame;
569
    frame_t * frame;
571
    frame = list_get_instance(block, frame_t, buddy_link);
570
    frame = list_get_instance(block, frame_t, buddy_link);
572
    frame->refcount = 1;
571
    frame->refcount = 1;
573
}
572
}
574
 
573
 
575
/** Prints list of zones
574
/** Prints list of zones
576
 *
575
 *
577
 */
576
 */
578
void zone_print_list(void) {
577
void zone_print_list(void) {
579
    zone_t *zone = NULL;
578
    zone_t *zone = NULL;
580
    link_t *cur;
579
    link_t *cur;
581
    ipl_t ipl;
580
    ipl_t ipl;
582
 
581
 
583
    ipl = interrupts_disable();
582
    ipl = interrupts_disable();
584
    spinlock_lock(&zone_head_lock);
583
    spinlock_lock(&zone_head_lock);
585
    printf("Base address\tFree Frames\tBusy Frames\n");
584
    printf("Base address\tFree Frames\tBusy Frames\n");
586
    printf("------------\t-----------\t-----------\n");
585
    printf("------------\t-----------\t-----------\n");
587
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
586
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
588
        zone = list_get_instance(cur, zone_t, link);
587
        zone = list_get_instance(cur, zone_t, link);
589
        spinlock_lock(&zone->lock);
588
        spinlock_lock(&zone->lock);
590
        printf("%L\t%d\t\t%d\n",zone->base, zone->free_count, zone->busy_count);
589
        printf("%L\t%d\t\t%d\n",zone->base, zone->free_count, zone->busy_count);
591
        spinlock_unlock(&zone->lock);
590
        spinlock_unlock(&zone->lock);
592
    }
591
    }
593
    spinlock_unlock(&zone_head_lock);
592
    spinlock_unlock(&zone_head_lock);
594
    interrupts_restore(ipl);
593
    interrupts_restore(ipl);
595
}
594
}
596
 
595
 
597
/** Prints zone details
596
/** Prints zone details
598
 *
597
 *
599
 * @param base Zone base address
598
 * @param base Zone base address
600
 */
599
 */
601
void zone_print_one(__address base) {
600
void zone_print_one(__address base) {
602
    zone_t *zone = NULL, *z ;
601
    zone_t *zone = NULL, *z ;
603
    link_t *cur;
602
    link_t *cur;
604
    ipl_t ipl;
603
    ipl_t ipl;
605
 
604
 
606
    ipl = interrupts_disable();
605
    ipl = interrupts_disable();
607
    spinlock_lock(&zone_head_lock);
606
    spinlock_lock(&zone_head_lock);
608
   
607
   
609
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
608
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
610
        z = list_get_instance(cur, zone_t, link);
609
        z = list_get_instance(cur, zone_t, link);
611
        if (base == z->base) {
610
        if (base == z->base) {
612
            zone = z;
611
            zone = z;
613
            break;
612
            break;
614
        }
613
        }
615
    }
614
    }
616
   
615
   
617
    if (!zone) {
616
    if (!zone) {
618
        spinlock_unlock(&zone_head_lock);
617
        spinlock_unlock(&zone_head_lock);
619
        interrupts_restore(ipl);
618
        interrupts_restore(ipl);
620
        printf("No zone with address %X\n", base);
619
        printf("No zone with address %X\n", base);
621
        return;
620
        return;
622
    }
621
    }
623
   
622
   
624
    spinlock_lock(&zone->lock);
623
    spinlock_lock(&zone->lock);
625
    printf("Memory zone information\n\n");
624
    printf("Memory zone information\n\n");
626
    printf("Zone base address: %P\n", zone->base);
625
    printf("Zone base address: %P\n", zone->base);
627
    printf("Zone size: %d frames (%dK)\n", zone->free_count + zone->busy_count, ((zone->free_count + zone->busy_count) * FRAME_SIZE) >> 10);
626
    printf("Zone size: %d frames (%dK)\n", zone->free_count + zone->busy_count, ((zone->free_count + zone->busy_count) * FRAME_SIZE) >> 10);
628
    printf("Allocated space: %d frames (%dK)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10);
627
    printf("Allocated space: %d frames (%dK)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10);
629
    printf("Available space: %d (%dK)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10);
628
    printf("Available space: %d (%dK)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10);
630
   
629
   
631
    printf("\nBuddy allocator structures:\n\n");
630
    printf("\nBuddy allocator structures:\n\n");
632
    buddy_system_structure_print(zone->buddy_system, FRAME_SIZE);
631
    buddy_system_structure_print(zone->buddy_system, FRAME_SIZE);
633
   
632
   
634
    spinlock_unlock(&zone->lock);
633
    spinlock_unlock(&zone->lock);
635
    spinlock_unlock(&zone_head_lock);
634
    spinlock_unlock(&zone_head_lock);
636
    interrupts_restore(ipl);
635
    interrupts_restore(ipl);
637
}
636
}
638
 
637
 
639
 
638