Subversion Repositories HelenOS

Rev

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

Rev 2089 Rev 2121
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
/** @addtogroup genericmm
30
/** @addtogroup genericmm
31
 * @{
31
 * @{
32
 */
32
 */
33
 
33
 
34
/**
34
/**
35
 * @file
35
 * @file
36
 * @brief   Physical frame allocator.
36
 * @brief   Physical frame allocator.
37
 *
37
 *
38
 * This file contains the physical frame allocator and memory zone management.
38
 * This file contains the physical frame allocator and memory zone management.
39
 * The frame allocator is built on top of the buddy allocator.
39
 * The frame allocator is built on top of the buddy allocator.
40
 *
40
 *
41
 * @see buddy.c
41
 * @see buddy.c
42
 */
42
 */
43
 
43
 
44
/*
44
/*
45
 * Locking order
45
 * Locking order
46
 *
46
 *
47
 * In order to access particular zone, the process must first lock
47
 * In order to access particular zone, the process must first lock
48
 * the zones.lock, then lock the zone and then unlock the zones.lock.
48
 * the zones.lock, then lock the zone and then unlock the zones.lock.
49
 * This insures, that we can fiddle with the zones in runtime without
49
 * This insures, that we can fiddle with the zones in runtime without
50
 * affecting the processes.
50
 * affecting the processes.
51
 *
51
 *
52
 */
52
 */
53
 
53
 
54
#include <arch/types.h>
54
#include <arch/types.h>
55
#include <mm/frame.h>
55
#include <mm/frame.h>
56
#include <mm/as.h>
56
#include <mm/as.h>
57
#include <panic.h>
57
#include <panic.h>
58
#include <debug.h>
58
#include <debug.h>
59
#include <adt/list.h>
59
#include <adt/list.h>
60
#include <synch/spinlock.h>
60
#include <synch/spinlock.h>
61
#include <arch/asm.h>
61
#include <arch/asm.h>
62
#include <arch.h>
62
#include <arch.h>
63
#include <print.h>
63
#include <print.h>
64
#include <align.h>
64
#include <align.h>
65
#include <mm/slab.h>
65
#include <mm/slab.h>
66
#include <bitops.h>
66
#include <bitops.h>
67
#include <macros.h>
67
#include <macros.h>
68
 
68
 
69
typedef struct {
69
typedef struct {
70
    count_t refcount;   /**< tracking of shared frames  */
70
    count_t refcount;   /**< tracking of shared frames  */
71
    uint8_t buddy_order;    /**< buddy system block order */
71
    uint8_t buddy_order;    /**< buddy system block order */
72
    link_t buddy_link;  /**< link to the next free block inside one order */
72
    link_t buddy_link;  /**< link to the next free block inside one order */
73
    void *parent;           /**< If allocated by slab, this points there */
73
    void *parent;           /**< If allocated by slab, this points there */
74
} frame_t;
74
} frame_t;
75
 
75
 
76
typedef struct {
76
typedef struct {
77
    SPINLOCK_DECLARE(lock); /**< this lock protects everything below */
77
    SPINLOCK_DECLARE(lock); /**< this lock protects everything below */
78
    pfn_t base;     /**< frame_no of the first frame in the frames array */
78
    pfn_t base;     /**< frame_no of the first frame in the frames array */
79
    count_t count;          /**< Size of zone */
79
    count_t count;          /**< Size of zone */
80
 
80
 
81
    frame_t *frames;    /**< array of frame_t structures in this zone */
81
    frame_t *frames;    /**< array of frame_t structures in this zone */
82
    count_t free_count; /**< number of free frame_t structures */
82
    count_t free_count; /**< number of free frame_t structures */
83
    count_t busy_count; /**< number of busy frame_t structures */
83
    count_t busy_count; /**< number of busy frame_t structures */
84
   
84
   
85
    buddy_system_t *buddy_system; /**< buddy system for the zone */
85
    buddy_system_t *buddy_system; /**< buddy system for the zone */
86
    int flags;
86
    int flags;
87
} zone_t;
87
} zone_t;
88
 
88
 
89
/*
89
/*
90
 * The zoneinfo.lock must be locked when accessing zoneinfo structure.
90
 * The zoneinfo.lock must be locked when accessing zoneinfo structure.
91
 * Some of the attributes in zone_t structures are 'read-only'
91
 * Some of the attributes in zone_t structures are 'read-only'
92
 */
92
 */
93
 
93
 
94
struct {
94
typedef struct {
95
    SPINLOCK_DECLARE(lock);
95
    SPINLOCK_DECLARE(lock);
96
    int count;
96
    int count;
97
    zone_t *info[ZONES_MAX];
97
    zone_t *info[ZONES_MAX];
98
} zones;
98
} zones_t;
-
 
99
 
-
 
100
static zones_t zones;
99
 
101
 
100
 
102
 
101
/*********************************/
103
/*********************************/
102
/* Helper functions */
104
/* Helper functions */
103
static inline index_t frame_index(zone_t *zone, frame_t *frame)
105
static inline index_t frame_index(zone_t *zone, frame_t *frame)
104
{
106
{
105
    return (index_t)(frame - zone->frames);
107
    return (index_t)(frame - zone->frames);
106
}
108
}
107
static inline index_t frame_index_abs(zone_t *zone, frame_t *frame)
109
static inline index_t frame_index_abs(zone_t *zone, frame_t *frame)
108
{
110
{
109
    return (index_t)(frame - zone->frames) + zone->base;
111
    return (index_t)(frame - zone->frames) + zone->base;
110
}
112
}
111
static inline int frame_index_valid(zone_t *zone, index_t index)
113
static inline int frame_index_valid(zone_t *zone, index_t index)
112
{
114
{
113
    return index >= 0 && index < zone->count;
115
    return index >= 0 && index < zone->count;
114
}
116
}
115
 
117
 
116
/** Compute pfn_t from frame_t pointer & zone pointer */
118
/** Compute pfn_t from frame_t pointer & zone pointer */
117
static index_t make_frame_index(zone_t *zone, frame_t *frame)
119
static index_t make_frame_index(zone_t *zone, frame_t *frame)
118
{
120
{
119
    return frame - zone->frames;
121
    return frame - zone->frames;
120
}
122
}
121
 
123
 
122
/** Initialize frame structure
124
/** Initialize frame structure
123
 *
125
 *
124
 * Initialize frame structure.
126
 * Initialize frame structure.
125
 *
127
 *
126
 * @param frame Frame structure to be initialized.
128
 * @param frame Frame structure to be initialized.
127
 */
129
 */
128
static void frame_initialize(frame_t *frame)
130
static void frame_initialize(frame_t *frame)
129
{
131
{
130
    frame->refcount = 1;
132
    frame->refcount = 1;
131
    frame->buddy_order = 0;
133
    frame->buddy_order = 0;
132
}
134
}
133
 
135
 
134
/*************************************/
136
/*************************************/
135
/* Zoneinfo functions */
137
/* Zoneinfo functions */
136
 
138
 
137
/**
139
/**
138
 * Insert-sort zone into zones list
140
 * Insert-sort zone into zones list
139
 *
141
 *
140
 * @param newzone New zone to be inserted into zone list
142
 * @param newzone New zone to be inserted into zone list
141
 * @return zone number on success, -1 on error
143
 * @return zone number on success, -1 on error
142
 */
144
 */
143
static int zones_add_zone(zone_t *newzone)
145
static int zones_add_zone(zone_t *newzone)
144
{
146
{
145
    int i,j;
147
    int i,j;
146
    ipl_t ipl;
148
    ipl_t ipl;
147
    zone_t *z;
149
    zone_t *z;
148
 
150
 
149
    ipl = interrupts_disable();
151
    ipl = interrupts_disable();
150
    spinlock_lock(&zones.lock);
152
    spinlock_lock(&zones.lock);
151
    /* Try to merge */
153
    /* Try to merge */
152
    if (zones.count + 1 == ZONES_MAX)
154
    if (zones.count + 1 == ZONES_MAX)
153
        panic("Maximum zone(%d) count exceeded.", ZONES_MAX);
155
        panic("Maximum zone(%d) count exceeded.", ZONES_MAX);
154
    for (i = 0; i < zones.count; i++) {
156
    for (i = 0; i < zones.count; i++) {
155
        /* Check for overflow */
157
        /* Check for overflow */
156
        z = zones.info[i];
158
        z = zones.info[i];
157
        if (overlaps(newzone->base,newzone->count,
159
        if (overlaps(newzone->base,newzone->count,
158
                 z->base, z->count)) {
160
                 z->base, z->count)) {
159
            printf("Zones overlap!\n");
161
            printf("Zones overlap!\n");
160
            return -1;
162
            return -1;
161
        }
163
        }
162
        if (newzone->base < z->base)
164
        if (newzone->base < z->base)
163
            break;
165
            break;
164
    }
166
    }
165
    /* Move other zones up */
167
    /* Move other zones up */
166
    for (j = i;j < zones.count; j++)
168
    for (j = i;j < zones.count; j++)
167
        zones.info[j + 1] = zones.info[j];
169
        zones.info[j + 1] = zones.info[j];
168
    zones.info[i] = newzone;
170
    zones.info[i] = newzone;
169
    zones.count++;
171
    zones.count++;
170
    spinlock_unlock(&zones.lock);
172
    spinlock_unlock(&zones.lock);
171
    interrupts_restore(ipl);
173
    interrupts_restore(ipl);
172
 
174
 
173
    return i;
175
    return i;
174
}
176
}
175
 
177
 
176
/**
178
/**
177
 * Try to find a zone where can we find the frame
179
 * Try to find a zone where can we find the frame
178
 
180
 
179
 * Assume interrupts are disabled.
181
 * Assume interrupts are disabled.
180
 
182
 
181
 * @param frame Frame number contained in zone
183
 * @param frame Frame number contained in zone
182
 * @param pzone If not null, it is used as zone hint. Zone index
184
 * @param pzone If not null, it is used as zone hint. Zone index
183
 *              is filled into the variable on success.
185
 *              is filled into the variable on success.
184
 * @return Pointer to locked zone containing frame
186
 * @return Pointer to locked zone containing frame
185
 */
187
 */
186
static zone_t * find_zone_and_lock(pfn_t frame, int *pzone)
188
static zone_t * find_zone_and_lock(pfn_t frame, int *pzone)
187
{
189
{
188
    int i;
190
    int i;
189
    int hint = pzone ? *pzone : 0;
191
    int hint = pzone ? *pzone : 0;
190
    zone_t *z;
192
    zone_t *z;
191
   
193
   
192
    spinlock_lock(&zones.lock);
194
    spinlock_lock(&zones.lock);
193
 
195
 
194
    if (hint >= zones.count || hint < 0)
196
    if (hint >= zones.count || hint < 0)
195
        hint = 0;
197
        hint = 0;
196
   
198
   
197
    i = hint;
199
    i = hint;
198
    do {
200
    do {
199
        z = zones.info[i];
201
        z = zones.info[i];
200
        spinlock_lock(&z->lock);
202
        spinlock_lock(&z->lock);
201
        if (z->base <= frame && z->base + z->count > frame) {
203
        if (z->base <= frame && z->base + z->count > frame) {
202
            spinlock_unlock(&zones.lock); /* Unlock the global lock */
204
            spinlock_unlock(&zones.lock); /* Unlock the global lock */
203
            if (pzone)
205
            if (pzone)
204
                *pzone = i;
206
                *pzone = i;
205
            return z;
207
            return z;
206
        }
208
        }
207
        spinlock_unlock(&z->lock);
209
        spinlock_unlock(&z->lock);
208
 
210
 
209
        i++;
211
        i++;
210
        if (i >= zones.count)
212
        if (i >= zones.count)
211
            i = 0;
213
            i = 0;
212
    } while(i != hint);
214
    } while(i != hint);
213
 
215
 
214
    spinlock_unlock(&zones.lock);
216
    spinlock_unlock(&zones.lock);
215
    return NULL;
217
    return NULL;
216
}
218
}
217
 
219
 
218
/** @return True if zone can allocate specified order */
220
/** @return True if zone can allocate specified order */
219
static int zone_can_alloc(zone_t *z, uint8_t order)
221
static int zone_can_alloc(zone_t *z, uint8_t order)
220
{
222
{
221
    return buddy_system_can_alloc(z->buddy_system, order);
223
    return buddy_system_can_alloc(z->buddy_system, order);
222
}
224
}
223
 
225
 
224
/** Find and lock zone that can allocate order frames.
226
/** Find and lock zone that can allocate order frames.
225
 *
227
 *
226
 * Assume interrupts are disabled.
228
 * Assume interrupts are disabled.
227
 *
229
 *
228
 * @param order Size (2^order) of free space we are trying to find
230
 * @param order Size (2^order) of free space we are trying to find
229
 * @param pzone Pointer to preferred zone or NULL, on return contains zone number
231
 * @param pzone Pointer to preferred zone or NULL, on return contains zone number
230
 */
232
 */
231
static zone_t * find_free_zone_and_lock(uint8_t order, int *pzone)
233
static zone_t * find_free_zone_and_lock(uint8_t order, int *pzone)
232
{
234
{
233
    int i;
235
    int i;
234
    zone_t *z;
236
    zone_t *z;
235
    int hint = pzone ? *pzone : 0;
237
    int hint = pzone ? *pzone : 0;
236
   
238
   
237
    spinlock_lock(&zones.lock);
239
    spinlock_lock(&zones.lock);
238
    if (hint >= zones.count)
240
    if (hint >= zones.count)
239
        hint = 0;
241
        hint = 0;
240
    i = hint;
242
    i = hint;
241
    do {
243
    do {
242
        z = zones.info[i];
244
        z = zones.info[i];
243
       
245
       
244
        spinlock_lock(&z->lock);
246
        spinlock_lock(&z->lock);
245
 
247
 
246
        /* Check if the zone has 2^order frames area available  */
248
        /* Check if the zone has 2^order frames area available  */
247
        if (zone_can_alloc(z, order)) {
249
        if (zone_can_alloc(z, order)) {
248
            spinlock_unlock(&zones.lock);
250
            spinlock_unlock(&zones.lock);
249
            if (pzone)
251
            if (pzone)
250
                *pzone = i;
252
                *pzone = i;
251
            return z;
253
            return z;
252
        }
254
        }
253
        spinlock_unlock(&z->lock);
255
        spinlock_unlock(&z->lock);
254
        if (++i >= zones.count)
256
        if (++i >= zones.count)
255
            i = 0;
257
            i = 0;
256
    } while(i != hint);
258
    } while(i != hint);
257
    spinlock_unlock(&zones.lock);
259
    spinlock_unlock(&zones.lock);
258
    return NULL;
260
    return NULL;
259
}
261
}
260
 
262
 
261
/**************************/
263
/**************************/
262
/* Buddy system functions */
264
/* Buddy system functions */
263
/**************************/
265
/**************************/
264
 
266
 
265
/** Buddy system find_block implementation
267
/** Buddy system find_block implementation
266
 *
268
 *
267
 * Find block that is parent of current list.
269
 * Find block that is parent of current list.
268
 * That means go to lower addresses, until such block is found
270
 * That means go to lower addresses, until such block is found
269
 *
271
 *
270
 * @param order - Order of parent must be different then this parameter!!
272
 * @param order - Order of parent must be different then this parameter!!
271
 */
273
 */
272
static link_t *zone_buddy_find_block(buddy_system_t *b, link_t *child,
274
static link_t *zone_buddy_find_block(buddy_system_t *b, link_t *child,
273
                     uint8_t order)
275
                     uint8_t order)
274
{
276
{
275
    frame_t * frame;
277
    frame_t * frame;
276
    zone_t * zone;
278
    zone_t * zone;
277
    index_t index;
279
    index_t index;
278
   
280
   
279
    frame = list_get_instance(child, frame_t, buddy_link);
281
    frame = list_get_instance(child, frame_t, buddy_link);
280
    zone = (zone_t *) b->data;
282
    zone = (zone_t *) b->data;
281
 
283
 
282
    index = frame_index(zone, frame);
284
    index = frame_index(zone, frame);
283
    do {
285
    do {
284
        if (zone->frames[index].buddy_order != order) {
286
        if (zone->frames[index].buddy_order != order) {
285
            return &zone->frames[index].buddy_link;
287
            return &zone->frames[index].buddy_link;
286
        }
288
        }
287
    } while(index-- > 0);
289
    } while(index-- > 0);
288
    return NULL;
290
    return NULL;
289
}
291
}
290
 
292
 
291
static void zone_buddy_print_id(buddy_system_t *b, link_t *block)
293
static void zone_buddy_print_id(buddy_system_t *b, link_t *block)
292
{
294
{
293
    frame_t * frame;
295
    frame_t * frame;
294
    zone_t * zone;
296
    zone_t * zone;
295
    index_t index;
297
    index_t index;
296
 
298
 
297
    frame = list_get_instance(block, frame_t, buddy_link);
299
    frame = list_get_instance(block, frame_t, buddy_link);
298
    zone = (zone_t *) b->data;
300
    zone = (zone_t *) b->data;
299
    index = frame_index(zone, frame);
301
    index = frame_index(zone, frame);
300
    printf("%zd", index);
302
    printf("%zd", index);
301
}                    
303
}                    
302
 
304
 
303
/** Buddy system find_buddy implementation
305
/** Buddy system find_buddy implementation
304
 *
306
 *
305
 * @param b Buddy system.
307
 * @param b Buddy system.
306
 * @param block Block for which buddy should be found
308
 * @param block Block for which buddy should be found
307
 *
309
 *
308
 * @return Buddy for given block if found
310
 * @return Buddy for given block if found
309
 */
311
 */
310
static link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block)
312
static link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * block)
311
{
313
{
312
    frame_t * frame;
314
    frame_t * frame;
313
    zone_t * zone;
315
    zone_t * zone;
314
    index_t index;
316
    index_t index;
315
    bool is_left, is_right;
317
    bool is_left, is_right;
316
 
318
 
317
    frame = list_get_instance(block, frame_t, buddy_link);
319
    frame = list_get_instance(block, frame_t, buddy_link);
318
    zone = (zone_t *) b->data;
320
    zone = (zone_t *) b->data;
319
    ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame), frame->buddy_order));
321
    ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame), frame->buddy_order));
320
   
322
   
321
    is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame);
323
    is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame);
322
    is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame);
324
    is_right = IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame);
323
 
325
 
324
    ASSERT(is_left ^ is_right);
326
    ASSERT(is_left ^ is_right);
325
    if (is_left) {
327
    if (is_left) {
326
        index = (frame_index(zone, frame)) + (1 << frame->buddy_order);
328
        index = (frame_index(zone, frame)) + (1 << frame->buddy_order);
327
    } else {    /* if (is_right) */
329
    } else {    /* if (is_right) */
328
        index = (frame_index(zone, frame)) - (1 << frame->buddy_order);
330
        index = (frame_index(zone, frame)) - (1 << frame->buddy_order);
329
    }
331
    }
330
   
332
   
331
    if (frame_index_valid(zone, index)) {
333
    if (frame_index_valid(zone, index)) {
332
        if (zone->frames[index].buddy_order == frame->buddy_order &&
334
        if (zone->frames[index].buddy_order == frame->buddy_order &&
333
            zone->frames[index].refcount == 0) {
335
            zone->frames[index].refcount == 0) {
334
            return &zone->frames[index].buddy_link;
336
            return &zone->frames[index].buddy_link;
335
        }
337
        }
336
    }
338
    }
337
 
339
 
338
    return NULL;   
340
    return NULL;   
339
}
341
}
340
 
342
 
341
/** Buddy system bisect implementation
343
/** Buddy system bisect implementation
342
 *
344
 *
343
 * @param b Buddy system.
345
 * @param b Buddy system.
344
 * @param block Block to bisect
346
 * @param block Block to bisect
345
 *
347
 *
346
 * @return right block
348
 * @return right block
347
 */
349
 */
348
static link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) {
350
static link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block) {
349
    frame_t * frame_l, * frame_r;
351
    frame_t * frame_l, * frame_r;
350
 
352
 
351
    frame_l = list_get_instance(block, frame_t, buddy_link);
353
    frame_l = list_get_instance(block, frame_t, buddy_link);
352
    frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
354
    frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
353
   
355
   
354
    return &frame_r->buddy_link;
356
    return &frame_r->buddy_link;
355
}
357
}
356
 
358
 
357
/** Buddy system coalesce implementation
359
/** Buddy system coalesce implementation
358
 *
360
 *
359
 * @param b Buddy system.
361
 * @param b Buddy system.
360
 * @param block_1 First block
362
 * @param block_1 First block
361
 * @param block_2 First block's buddy
363
 * @param block_2 First block's buddy
362
 *
364
 *
363
 * @return Coalesced block (actually block that represents lower address)
365
 * @return Coalesced block (actually block that represents lower address)
364
 */
366
 */
365
static link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1,
367
static link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * block_1,
366
                    link_t * block_2)
368
                    link_t * block_2)
367
{
369
{
368
    frame_t *frame1, *frame2;
370
    frame_t *frame1, *frame2;
369
   
371
   
370
    frame1 = list_get_instance(block_1, frame_t, buddy_link);
372
    frame1 = list_get_instance(block_1, frame_t, buddy_link);
371
    frame2 = list_get_instance(block_2, frame_t, buddy_link);
373
    frame2 = list_get_instance(block_2, frame_t, buddy_link);
372
   
374
   
373
    return frame1 < frame2 ? block_1 : block_2;
375
    return frame1 < frame2 ? block_1 : block_2;
374
}
376
}
375
 
377
 
376
/** Buddy system set_order implementation
378
/** Buddy system set_order implementation
377
 *
379
 *
378
 * @param b Buddy system.
380
 * @param b Buddy system.
379
 * @param block Buddy system block
381
 * @param block Buddy system block
380
 * @param order Order to set
382
 * @param order Order to set
381
 */
383
 */
382
static void zone_buddy_set_order(buddy_system_t *b, link_t * block, uint8_t order) {
384
static void zone_buddy_set_order(buddy_system_t *b, link_t * block, uint8_t order) {
383
    frame_t * frame;
385
    frame_t * frame;
384
    frame = list_get_instance(block, frame_t, buddy_link);
386
    frame = list_get_instance(block, frame_t, buddy_link);
385
    frame->buddy_order = order;
387
    frame->buddy_order = order;
386
}
388
}
387
 
389
 
388
/** Buddy system get_order implementation
390
/** Buddy system get_order implementation
389
 *
391
 *
390
 * @param b Buddy system.
392
 * @param b Buddy system.
391
 * @param block Buddy system block
393
 * @param block Buddy system block
392
 *
394
 *
393
 * @return Order of block
395
 * @return Order of block
394
 */
396
 */
395
static uint8_t zone_buddy_get_order(buddy_system_t *b, link_t * block) {
397
static uint8_t zone_buddy_get_order(buddy_system_t *b, link_t * block) {
396
    frame_t * frame;
398
    frame_t * frame;
397
    frame = list_get_instance(block, frame_t, buddy_link);
399
    frame = list_get_instance(block, frame_t, buddy_link);
398
    return frame->buddy_order;
400
    return frame->buddy_order;
399
}
401
}
400
 
402
 
401
/** Buddy system mark_busy implementation
403
/** Buddy system mark_busy implementation
402
 *
404
 *
403
 * @param b Buddy system
405
 * @param b Buddy system
404
 * @param block Buddy system block
406
 * @param block Buddy system block
405
 *
407
 *
406
 */
408
 */
407
static void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) {
409
static void zone_buddy_mark_busy(buddy_system_t *b, link_t * block) {
408
    frame_t * frame;
410
    frame_t * frame;
409
 
411
 
410
    frame = list_get_instance(block, frame_t, buddy_link);
412
    frame = list_get_instance(block, frame_t, buddy_link);
411
    frame->refcount = 1;
413
    frame->refcount = 1;
412
}
414
}
413
 
415
 
414
/** Buddy system mark_available implementation
416
/** Buddy system mark_available implementation
415
 *
417
 *
416
 * @param b Buddy system
418
 * @param b Buddy system
417
 * @param block Buddy system block
419
 * @param block Buddy system block
418
 *
420
 *
419
 */
421
 */
420
static void zone_buddy_mark_available(buddy_system_t *b, link_t * block) {
422
static void zone_buddy_mark_available(buddy_system_t *b, link_t * block) {
421
    frame_t * frame;
423
    frame_t * frame;
422
    frame = list_get_instance(block, frame_t, buddy_link);
424
    frame = list_get_instance(block, frame_t, buddy_link);
423
    frame->refcount = 0;
425
    frame->refcount = 0;
424
}
426
}
425
 
427
 
426
static buddy_system_operations_t zone_buddy_system_operations = {
428
static buddy_system_operations_t zone_buddy_system_operations = {
427
    .find_buddy = zone_buddy_find_buddy,
429
    .find_buddy = zone_buddy_find_buddy,
428
    .bisect = zone_buddy_bisect,
430
    .bisect = zone_buddy_bisect,
429
    .coalesce = zone_buddy_coalesce,
431
    .coalesce = zone_buddy_coalesce,
430
    .set_order = zone_buddy_set_order,
432
    .set_order = zone_buddy_set_order,
431
    .get_order = zone_buddy_get_order,
433
    .get_order = zone_buddy_get_order,
432
    .mark_busy = zone_buddy_mark_busy,
434
    .mark_busy = zone_buddy_mark_busy,
433
    .mark_available = zone_buddy_mark_available,
435
    .mark_available = zone_buddy_mark_available,
434
    .find_block = zone_buddy_find_block,
436
    .find_block = zone_buddy_find_block,
435
    .print_id = zone_buddy_print_id
437
    .print_id = zone_buddy_print_id
436
};
438
};
437
 
439
 
438
/******************/
440
/******************/
439
/* Zone functions */
441
/* Zone functions */
440
/******************/
442
/******************/
441
 
443
 
442
/** Allocate frame in particular zone
444
/** Allocate frame in particular zone
443
 *
445
 *
444
 * Assume zone is locked
446
 * Assume zone is locked
445
 * Panics if allocation is impossible.
447
 * Panics if allocation is impossible.
446
 *
448
 *
447
 * @param zone  Zone to allocate from.
449
 * @param zone  Zone to allocate from.
448
 * @param order Allocate exactly 2^order frames.
450
 * @param order Allocate exactly 2^order frames.
449
 *
451
 *
450
 * @return Frame index in zone
452
 * @return Frame index in zone
451
 *
453
 *
452
 */
454
 */
453
static pfn_t zone_frame_alloc(zone_t *zone, uint8_t order)
455
static pfn_t zone_frame_alloc(zone_t *zone, uint8_t order)
454
{
456
{
455
    pfn_t v;
457
    pfn_t v;
456
    link_t *tmp;
458
    link_t *tmp;
457
    frame_t *frame;
459
    frame_t *frame;
458
 
460
 
459
    /* Allocate frames from zone buddy system */
461
    /* Allocate frames from zone buddy system */
460
    tmp = buddy_system_alloc(zone->buddy_system, order);
462
    tmp = buddy_system_alloc(zone->buddy_system, order);
461
   
463
   
462
    ASSERT(tmp);
464
    ASSERT(tmp);
463
   
465
   
464
    /* Update zone information. */
466
    /* Update zone information. */
465
    zone->free_count -= (1 << order);
467
    zone->free_count -= (1 << order);
466
    zone->busy_count += (1 << order);
468
    zone->busy_count += (1 << order);
467
 
469
 
468
    /* Frame will be actually a first frame of the block. */
470
    /* Frame will be actually a first frame of the block. */
469
    frame = list_get_instance(tmp, frame_t, buddy_link);
471
    frame = list_get_instance(tmp, frame_t, buddy_link);
470
   
472
   
471
    /* get frame address */
473
    /* get frame address */
472
    v = make_frame_index(zone, frame);
474
    v = make_frame_index(zone, frame);
473
    return v;
475
    return v;
474
}
476
}
475
 
477
 
476
/** Free frame from zone
478
/** Free frame from zone
477
 *
479
 *
478
 * Assume zone is locked
480
 * Assume zone is locked
479
 *
481
 *
480
 * @param zone Pointer to zone from which the frame is to be freed
482
 * @param zone Pointer to zone from which the frame is to be freed
481
 * @param frame_idx Frame index relative to zone
483
 * @param frame_idx Frame index relative to zone
482
 */
484
 */
483
static void zone_frame_free(zone_t *zone, index_t frame_idx)
485
static void zone_frame_free(zone_t *zone, index_t frame_idx)
484
{
486
{
485
    frame_t *frame;
487
    frame_t *frame;
486
    uint8_t order;
488
    uint8_t order;
487
 
489
 
488
    frame = &zone->frames[frame_idx];
490
    frame = &zone->frames[frame_idx];
489
   
491
   
490
    /* remember frame order */
492
    /* remember frame order */
491
    order = frame->buddy_order;
493
    order = frame->buddy_order;
492
 
494
 
493
    ASSERT(frame->refcount);
495
    ASSERT(frame->refcount);
494
 
496
 
495
    if (!--frame->refcount) {
497
    if (!--frame->refcount) {
496
        buddy_system_free(zone->buddy_system, &frame->buddy_link);
498
        buddy_system_free(zone->buddy_system, &frame->buddy_link);
497
   
499
   
498
        /* Update zone information. */
500
        /* Update zone information. */
499
        zone->free_count += (1 << order);
501
        zone->free_count += (1 << order);
500
        zone->busy_count -= (1 << order);
502
        zone->busy_count -= (1 << order);
501
    }
503
    }
502
}
504
}
503
 
505
 
504
/** Return frame from zone */
506
/** Return frame from zone */
505
static frame_t * zone_get_frame(zone_t *zone, index_t frame_idx)
507
static frame_t * zone_get_frame(zone_t *zone, index_t frame_idx)
506
{
508
{
507
    ASSERT(frame_idx < zone->count);
509
    ASSERT(frame_idx < zone->count);
508
    return &zone->frames[frame_idx];
510
    return &zone->frames[frame_idx];
509
}
511
}
510
 
512
 
511
/** Mark frame in zone unavailable to allocation */
513
/** Mark frame in zone unavailable to allocation */
512
static void zone_mark_unavailable(zone_t *zone, index_t frame_idx)
514
static void zone_mark_unavailable(zone_t *zone, index_t frame_idx)
513
{
515
{
514
    frame_t *frame;
516
    frame_t *frame;
515
    link_t *link;
517
    link_t *link;
516
 
518
 
517
    frame = zone_get_frame(zone, frame_idx);
519
    frame = zone_get_frame(zone, frame_idx);
518
    if (frame->refcount)
520
    if (frame->refcount)
519
        return;
521
        return;
520
    link = buddy_system_alloc_block(zone->buddy_system,
522
    link = buddy_system_alloc_block(zone->buddy_system,
521
                    &frame->buddy_link);
523
                    &frame->buddy_link);
522
    ASSERT(link);
524
    ASSERT(link);
523
    zone->free_count--;
525
    zone->free_count--;
524
}
526
}
525
 
527
 
526
/**
528
/**
527
 * Join 2 zones
529
 * Join 2 zones
528
 *
530
 *
529
 * Expect zone_t *z to point to space at least zone_conf_size large
531
 * Expect zone_t *z to point to space at least zone_conf_size large
530
 *
532
 *
531
 * Assume z1 & z2 are locked
533
 * Assume z1 & z2 are locked
532
 *
534
 *
533
 * @param z Target zone structure pointer
535
 * @param z Target zone structure pointer
534
 * @param z1 Zone to merge
536
 * @param z1 Zone to merge
535
 * @param z2 Zone to merge
537
 * @param z2 Zone to merge
536
 */
538
 */
537
static void _zone_merge(zone_t *z, zone_t *z1, zone_t *z2)
539
static void _zone_merge(zone_t *z, zone_t *z1, zone_t *z2)
538
{
540
{
539
    uint8_t max_order;
541
    uint8_t max_order;
540
    int i, z2idx;
542
    int i, z2idx;
541
    pfn_t frame_idx;
543
    pfn_t frame_idx;
542
    frame_t *frame;
544
    frame_t *frame;
543
 
545
 
544
    ASSERT(!overlaps(z1->base,z1->count,z2->base,z2->count));
546
    ASSERT(!overlaps(z1->base,z1->count,z2->base,z2->count));
545
    ASSERT(z1->base < z2->base);
547
    ASSERT(z1->base < z2->base);
546
 
548
 
547
    spinlock_initialize(&z->lock, "zone_lock");
549
    spinlock_initialize(&z->lock, "zone_lock");
548
    z->base = z1->base;
550
    z->base = z1->base;
549
    z->count = z2->base+z2->count - z1->base;
551
    z->count = z2->base+z2->count - z1->base;
550
    z->flags = z1->flags & z2->flags;
552
    z->flags = z1->flags & z2->flags;
551
 
553
 
552
    z->free_count = z1->free_count + z2->free_count;
554
    z->free_count = z1->free_count + z2->free_count;
553
    z->busy_count = z1->busy_count + z2->busy_count;
555
    z->busy_count = z1->busy_count + z2->busy_count;
554
   
556
   
555
    max_order = fnzb(z->count);
557
    max_order = fnzb(z->count);
556
 
558
 
557
    z->buddy_system = (buddy_system_t *)&z[1];
559
    z->buddy_system = (buddy_system_t *)&z[1];
558
    buddy_system_create(z->buddy_system, max_order,
560
    buddy_system_create(z->buddy_system, max_order,
559
                &zone_buddy_system_operations,
561
                &zone_buddy_system_operations,
560
                (void *) z);
562
                (void *) z);
561
 
563
 
562
    z->frames = (frame_t *)((void *)z->buddy_system+buddy_conf_size(max_order));
564
    z->frames = (frame_t *)((void *)z->buddy_system+buddy_conf_size(max_order));
563
    for (i = 0; i < z->count; i++) {
565
    for (i = 0; i < z->count; i++) {
564
        /* This marks all frames busy */
566
        /* This marks all frames busy */
565
        frame_initialize(&z->frames[i]);
567
        frame_initialize(&z->frames[i]);
566
    }
568
    }
567
    /* Copy frames from both zones to preserve full frame orders,
569
    /* Copy frames from both zones to preserve full frame orders,
568
     * parents etc. Set all free frames with refcount=0 to 1, because
570
     * parents etc. Set all free frames with refcount=0 to 1, because
569
     * we add all free frames to buddy allocator later again, clear
571
     * we add all free frames to buddy allocator later again, clear
570
     * order to 0. Don't set busy frames with refcount=0, as they
572
     * order to 0. Don't set busy frames with refcount=0, as they
571
     * will not be reallocated during merge and it would make later
573
     * will not be reallocated during merge and it would make later
572
     * problems with allocation/free.
574
     * problems with allocation/free.
573
     */
575
     */
574
    for (i=0; i<z1->count; i++)
576
    for (i=0; i<z1->count; i++)
575
        z->frames[i] = z1->frames[i];
577
        z->frames[i] = z1->frames[i];
576
    for (i=0; i < z2->count; i++) {
578
    for (i=0; i < z2->count; i++) {
577
        z2idx = i + (z2->base - z1->base);
579
        z2idx = i + (z2->base - z1->base);
578
        z->frames[z2idx] = z2->frames[i];
580
        z->frames[z2idx] = z2->frames[i];
579
    }
581
    }
580
    i = 0;
582
    i = 0;
581
    while (i < z->count) {
583
    while (i < z->count) {
582
        if (z->frames[i].refcount) {
584
        if (z->frames[i].refcount) {
583
            /* skip busy frames */
585
            /* skip busy frames */
584
            i += 1 << z->frames[i].buddy_order;
586
            i += 1 << z->frames[i].buddy_order;
585
        } else { /* Free frames, set refcount=1 */
587
        } else { /* Free frames, set refcount=1 */
586
            /* All free frames have refcount=0, we need not
588
            /* All free frames have refcount=0, we need not
587
             * to check the order */
589
             * to check the order */
588
            z->frames[i].refcount = 1;
590
            z->frames[i].refcount = 1;
589
            z->frames[i].buddy_order = 0;
591
            z->frames[i].buddy_order = 0;
590
            i++;
592
            i++;
591
        }
593
        }
592
    }
594
    }
593
    /* Add free blocks from the 2 original zones */
595
    /* Add free blocks from the 2 original zones */
594
    while (zone_can_alloc(z1, 0)) {
596
    while (zone_can_alloc(z1, 0)) {
595
        frame_idx = zone_frame_alloc(z1, 0);
597
        frame_idx = zone_frame_alloc(z1, 0);
596
        frame = &z->frames[frame_idx];
598
        frame = &z->frames[frame_idx];
597
        frame->refcount = 0;
599
        frame->refcount = 0;
598
        buddy_system_free(z->buddy_system, &frame->buddy_link);
600
        buddy_system_free(z->buddy_system, &frame->buddy_link);
599
    }
601
    }
600
    while (zone_can_alloc(z2, 0)) {
602
    while (zone_can_alloc(z2, 0)) {
601
        frame_idx = zone_frame_alloc(z2, 0);
603
        frame_idx = zone_frame_alloc(z2, 0);
602
        frame = &z->frames[frame_idx + (z2->base-z1->base)];
604
        frame = &z->frames[frame_idx + (z2->base-z1->base)];
603
        frame->refcount = 0;
605
        frame->refcount = 0;
604
        buddy_system_free(z->buddy_system, &frame->buddy_link);
606
        buddy_system_free(z->buddy_system, &frame->buddy_link);
605
    }
607
    }
606
}
608
}
607
 
609
 
608
/** Return old configuration frames into the zone
610
/** Return old configuration frames into the zone
609
 *
611
 *
610
 * We have several cases
612
 * We have several cases
611
 * - the conf. data is outside of zone -> exit, shall we call frame_free??
613
 * - the conf. data is outside of zone -> exit, shall we call frame_free??
612
 * - the conf. data was created by zone_create or
614
 * - the conf. data was created by zone_create or
613
 *   updated with reduce_region -> free every frame
615
 *   updated with reduce_region -> free every frame
614
 *
616
 *
615
 * @param newzone The actual zone where freeing should occur
617
 * @param newzone The actual zone where freeing should occur
616
 * @param oldzone Pointer to old zone configuration data that should
618
 * @param oldzone Pointer to old zone configuration data that should
617
 *                be freed from new zone
619
 *                be freed from new zone
618
 */
620
 */
619
static void return_config_frames(zone_t *newzone, zone_t *oldzone)
621
static void return_config_frames(zone_t *newzone, zone_t *oldzone)
620
{
622
{
621
    pfn_t pfn;
623
    pfn_t pfn;
622
    frame_t *frame;
624
    frame_t *frame;
623
    count_t cframes;
625
    count_t cframes;
624
    int i;
626
    int i;
625
 
627
 
626
    pfn = ADDR2PFN((uintptr_t)KA2PA(oldzone));
628
    pfn = ADDR2PFN((uintptr_t)KA2PA(oldzone));
627
    cframes = SIZE2FRAMES(zone_conf_size(oldzone->count));
629
    cframes = SIZE2FRAMES(zone_conf_size(oldzone->count));
628
   
630
   
629
    if (pfn < newzone->base || pfn >= newzone->base + newzone->count)
631
    if (pfn < newzone->base || pfn >= newzone->base + newzone->count)
630
        return;
632
        return;
631
 
633
 
632
    frame = &newzone->frames[pfn - newzone->base];
634
    frame = &newzone->frames[pfn - newzone->base];
633
    ASSERT(!frame->buddy_order);
635
    ASSERT(!frame->buddy_order);
634
 
636
 
635
    for (i=0; i < cframes; i++) {
637
    for (i=0; i < cframes; i++) {
636
        newzone->busy_count++;
638
        newzone->busy_count++;
637
        zone_frame_free(newzone, pfn+i-newzone->base);
639
        zone_frame_free(newzone, pfn+i-newzone->base);
638
    }
640
    }
639
}
641
}
640
 
642
 
641
/** Reduce allocated block to count of order 0 frames
643
/** Reduce allocated block to count of order 0 frames
642
 *
644
 *
643
 * The allocated block need 2^order frames of space. Reduce all frames
645
 * The allocated block need 2^order frames of space. Reduce all frames
644
 * in block to order 0 and free the unneeded frames. This means, that
646
 * in block to order 0 and free the unneeded frames. This means, that
645
 * when freeing the previously allocated block starting with frame_idx,
647
 * when freeing the previously allocated block starting with frame_idx,
646
 * you have to free every frame.
648
 * you have to free every frame.
647
 *
649
 *
648
 * @param zone
650
 * @param zone
649
 * @param frame_idx Index to block
651
 * @param frame_idx Index to block
650
 * @param count Allocated space in block
652
 * @param count Allocated space in block
651
 */
653
 */
652
static void zone_reduce_region(zone_t *zone, pfn_t frame_idx, count_t count)
654
static void zone_reduce_region(zone_t *zone, pfn_t frame_idx, count_t count)
653
{
655
{
654
    count_t i;
656
    count_t i;
655
    uint8_t order;
657
    uint8_t order;
656
    frame_t *frame;
658
    frame_t *frame;
657
   
659
   
658
    ASSERT(frame_idx+count < zone->count);
660
    ASSERT(frame_idx+count < zone->count);
659
 
661
 
660
    order = zone->frames[frame_idx].buddy_order;
662
    order = zone->frames[frame_idx].buddy_order;
661
    ASSERT((1 << order) >= count);
663
    ASSERT((1 << order) >= count);
662
 
664
 
663
    /* Reduce all blocks to order 0 */
665
    /* Reduce all blocks to order 0 */
664
    for (i=0; i < (1 << order); i++) {
666
    for (i=0; i < (1 << order); i++) {
665
        frame = &zone->frames[i + frame_idx];
667
        frame = &zone->frames[i + frame_idx];
666
        frame->buddy_order = 0;
668
        frame->buddy_order = 0;
667
        if (! frame->refcount)
669
        if (! frame->refcount)
668
            frame->refcount = 1;
670
            frame->refcount = 1;
669
        ASSERT(frame->refcount == 1);
671
        ASSERT(frame->refcount == 1);
670
    }
672
    }
671
    /* Free unneeded frames */
673
    /* Free unneeded frames */
672
    for (i=count; i < (1 << order); i++) {
674
    for (i=count; i < (1 << order); i++) {
673
        zone_frame_free(zone, i + frame_idx);
675
        zone_frame_free(zone, i + frame_idx);
674
    }
676
    }
675
}
677
}
676
 
678
 
677
/** Merge zones z1 and z2
679
/** Merge zones z1 and z2
678
 *
680
 *
679
 * - the zones must be 2 zones with no zone existing in between,
681
 * - the zones must be 2 zones with no zone existing in between,
680
 *   which means that z2 = z1+1
682
 *   which means that z2 = z1+1
681
 *
683
 *
682
 * - When you create a new zone, the frame allocator configuration does
684
 * - When you create a new zone, the frame allocator configuration does
683
 *   not to be 2^order size. Once the allocator is running it is no longer
685
 *   not to be 2^order size. Once the allocator is running it is no longer
684
 *   possible, merged configuration data occupies more space :-/
686
 *   possible, merged configuration data occupies more space :-/
685
 */
687
 */
686
void zone_merge(int z1, int z2)
688
void zone_merge(int z1, int z2)
687
{
689
{
688
    ipl_t ipl;
690
    ipl_t ipl;
689
    zone_t *zone1, *zone2, *newzone;
691
    zone_t *zone1, *zone2, *newzone;
690
    int cframes;
692
    int cframes;
691
    uint8_t order;
693
    uint8_t order;
692
    int i;
694
    int i;
693
    pfn_t pfn;
695
    pfn_t pfn;
694
 
696
 
695
    ipl = interrupts_disable();
697
    ipl = interrupts_disable();
696
    spinlock_lock(&zones.lock);
698
    spinlock_lock(&zones.lock);
697
 
699
 
698
    if (z1 < 0 || z1 >= zones.count || z2 < 0 || z2 >= zones.count)
700
    if (z1 < 0 || z1 >= zones.count || z2 < 0 || z2 >= zones.count)
699
        goto errout;
701
        goto errout;
700
    /* We can join only 2 zones with none existing inbetween */
702
    /* We can join only 2 zones with none existing inbetween */
701
    if (z2-z1 != 1)
703
    if (z2-z1 != 1)
702
        goto errout;
704
        goto errout;
703
 
705
 
704
    zone1 = zones.info[z1];
706
    zone1 = zones.info[z1];
705
    zone2 = zones.info[z2];
707
    zone2 = zones.info[z2];
706
    spinlock_lock(&zone1->lock);
708
    spinlock_lock(&zone1->lock);
707
    spinlock_lock(&zone2->lock);
709
    spinlock_lock(&zone2->lock);
708
 
710
 
709
    cframes = SIZE2FRAMES(zone_conf_size(zone2->base+zone2->count-zone1->base));
711
    cframes = SIZE2FRAMES(zone_conf_size(zone2->base+zone2->count-zone1->base));
710
    if (cframes == 1)
712
    if (cframes == 1)
711
        order = 0;
713
        order = 0;
712
    else
714
    else
713
        order = fnzb(cframes - 1) + 1;
715
        order = fnzb(cframes - 1) + 1;
714
 
716
 
715
    /* Allocate zonedata inside one of the zones */
717
    /* Allocate zonedata inside one of the zones */
716
    if (zone_can_alloc(zone1, order))
718
    if (zone_can_alloc(zone1, order))
717
        pfn = zone1->base + zone_frame_alloc(zone1, order);
719
        pfn = zone1->base + zone_frame_alloc(zone1, order);
718
    else if (zone_can_alloc(zone2, order))
720
    else if (zone_can_alloc(zone2, order))
719
        pfn = zone2->base + zone_frame_alloc(zone2, order);
721
        pfn = zone2->base + zone_frame_alloc(zone2, order);
720
    else
722
    else
721
        goto errout2;
723
        goto errout2;
722
 
724
 
723
    newzone = (zone_t *)PA2KA(PFN2ADDR(pfn));
725
    newzone = (zone_t *)PA2KA(PFN2ADDR(pfn));
724
 
726
 
725
    _zone_merge(newzone, zone1, zone2);
727
    _zone_merge(newzone, zone1, zone2);
726
 
728
 
727
    /* Free unneeded config frames */
729
    /* Free unneeded config frames */
728
    zone_reduce_region(newzone, pfn - newzone->base,  cframes);
730
    zone_reduce_region(newzone, pfn - newzone->base,  cframes);
729
    /* Subtract zone information from busy frames */
731
    /* Subtract zone information from busy frames */
730
    newzone->busy_count -= cframes;
732
    newzone->busy_count -= cframes;
731
 
733
 
732
    /* Replace existing zones in zoneinfo list */
734
    /* Replace existing zones in zoneinfo list */
733
    zones.info[z1] = newzone;
735
    zones.info[z1] = newzone;
734
    for (i = z2 + 1; i < zones.count; i++)
736
    for (i = z2 + 1; i < zones.count; i++)
735
        zones.info[i - 1] = zones.info[i];
737
        zones.info[i - 1] = zones.info[i];
736
    zones.count--;
738
    zones.count--;
737
 
739
 
738
    /* Free old zone information */
740
    /* Free old zone information */
739
    return_config_frames(newzone, zone1);
741
    return_config_frames(newzone, zone1);
740
    return_config_frames(newzone, zone2);
742
    return_config_frames(newzone, zone2);
741
errout2:
743
errout2:
742
    /* Nobody is allowed to enter to zone, so we are safe
744
    /* Nobody is allowed to enter to zone, so we are safe
743
     * to touch the spinlocks last time */
745
     * to touch the spinlocks last time */
744
    spinlock_unlock(&zone1->lock);
746
    spinlock_unlock(&zone1->lock);
745
    spinlock_unlock(&zone2->lock);
747
    spinlock_unlock(&zone2->lock);
746
errout:
748
errout:
747
    spinlock_unlock(&zones.lock);
749
    spinlock_unlock(&zones.lock);
748
    interrupts_restore(ipl);
750
    interrupts_restore(ipl);
749
}
751
}
750
 
752
 
751
/**
753
/**
752
 * Merge all zones into one big zone
754
 * Merge all zones into one big zone
753
 *
755
 *
754
 * It is reasonable to do this on systems whose bios reports parts in chunks,
756
 * It is reasonable to do this on systems whose bios reports parts in chunks,
755
 * so that we could have 1 zone (it's faster).
757
 * so that we could have 1 zone (it's faster).
756
 */
758
 */
757
void zone_merge_all(void)
759
void zone_merge_all(void)
758
{
760
{
759
    int count = zones.count;
761
    int count = zones.count;
760
 
762
 
761
    while (zones.count > 1 && --count) {
763
    while (zones.count > 1 && --count) {
762
        zone_merge(0,1);
764
        zone_merge(0,1);
763
        break;
765
        break;
764
    }
766
    }
765
}
767
}
766
 
768
 
767
/** Create frame zone
769
/** Create frame zone
768
 *
770
 *
769
 * Create new frame zone.
771
 * Create new frame zone.
770
 *
772
 *
771
 * @param start Physical address of the first frame within the zone.
773
 * @param start Physical address of the first frame within the zone.
772
 * @param count Count of frames in zone
774
 * @param count Count of frames in zone
773
 * @param z Address of configuration information of zone
775
 * @param z Address of configuration information of zone
774
 * @param flags Zone flags.
776
 * @param flags Zone flags.
775
 *
777
 *
776
 * @return Initialized zone.
778
 * @return Initialized zone.
777
 */
779
 */
778
static void zone_construct(pfn_t start, count_t count, zone_t *z, int flags)
780
static void zone_construct(pfn_t start, count_t count, zone_t *z, int flags)
779
{
781
{
780
    int i;
782
    int i;
781
    uint8_t max_order;
783
    uint8_t max_order;
782
 
784
 
783
    spinlock_initialize(&z->lock, "zone_lock");
785
    spinlock_initialize(&z->lock, "zone_lock");
784
    z->base = start;
786
    z->base = start;
785
    z->count = count;
787
    z->count = count;
786
    z->flags = flags;
788
    z->flags = flags;
787
    z->free_count = count;
789
    z->free_count = count;
788
    z->busy_count = 0;
790
    z->busy_count = 0;
789
 
791
 
790
    /*
792
    /*
791
     * Compute order for buddy system, initialize
793
     * Compute order for buddy system, initialize
792
     */
794
     */
793
    max_order = fnzb(count);
795
    max_order = fnzb(count);
794
    z->buddy_system = (buddy_system_t *)&z[1];
796
    z->buddy_system = (buddy_system_t *)&z[1];
795
   
797
   
796
    buddy_system_create(z->buddy_system, max_order,
798
    buddy_system_create(z->buddy_system, max_order,
797
                &zone_buddy_system_operations,
799
                &zone_buddy_system_operations,
798
                (void *) z);
800
                (void *) z);
799
   
801
   
800
    /* Allocate frames _after_ the conframe */
802
    /* Allocate frames _after_ the conframe */
801
    /* Check sizes */
803
    /* Check sizes */
802
    z->frames = (frame_t *)((void *)z->buddy_system+buddy_conf_size(max_order));
804
    z->frames = (frame_t *)((void *)z->buddy_system+buddy_conf_size(max_order));
803
    for (i = 0; i<count; i++) {
805
    for (i = 0; i<count; i++) {
804
        frame_initialize(&z->frames[i]);
806
        frame_initialize(&z->frames[i]);
805
    }
807
    }
806
   
808
   
807
    /* Stuffing frames */
809
    /* Stuffing frames */
808
    for (i = 0; i < count; i++) {
810
    for (i = 0; i < count; i++) {
809
        z->frames[i].refcount = 0;
811
        z->frames[i].refcount = 0;
810
        buddy_system_free(z->buddy_system, &z->frames[i].buddy_link);
812
        buddy_system_free(z->buddy_system, &z->frames[i].buddy_link);
811
    }
813
    }
812
}
814
}
813
 
815
 
814
/** Compute configuration data size for zone
816
/** Compute configuration data size for zone
815
 *
817
 *
816
 * @param count Size of zone in frames
818
 * @param count Size of zone in frames
817
 * @return Size of zone configuration info (in bytes)
819
 * @return Size of zone configuration info (in bytes)
818
 */
820
 */
819
uintptr_t zone_conf_size(count_t count)
821
uintptr_t zone_conf_size(count_t count)
820
{
822
{
821
    int size = sizeof(zone_t) + count*sizeof(frame_t);
823
    int size = sizeof(zone_t) + count*sizeof(frame_t);
822
    int max_order;
824
    int max_order;
823
 
825
 
824
    max_order = fnzb(count);
826
    max_order = fnzb(count);
825
    size += buddy_conf_size(max_order);
827
    size += buddy_conf_size(max_order);
826
    return size;
828
    return size;
827
}
829
}
828
 
830
 
829
/** Create and add zone to system
831
/** Create and add zone to system
830
 *
832
 *
831
 * @param start First frame number (absolute)
833
 * @param start First frame number (absolute)
832
 * @param count Size of zone in frames
834
 * @param count Size of zone in frames
833
 * @param confframe Where configuration frames are supposed to be.
835
 * @param confframe Where configuration frames are supposed to be.
834
 *                  Automatically checks, that we will not disturb the
836
 *                  Automatically checks, that we will not disturb the
835
 *                  kernel and possibly init.
837
 *                  kernel and possibly init.
836
 *                  If confframe is given _outside_ this zone, it is expected,
838
 *                  If confframe is given _outside_ this zone, it is expected,
837
 *                  that the area is already marked BUSY and big enough
839
 *                  that the area is already marked BUSY and big enough
838
 *                  to contain zone_conf_size() amount of data.
840
 *                  to contain zone_conf_size() amount of data.
839
 *                  If the confframe is inside the area, the zone free frame
841
 *                  If the confframe is inside the area, the zone free frame
840
 *                  information is modified not to include it.
842
 *                  information is modified not to include it.
841
 *
843
 *
842
 * @return Zone number or -1 on error
844
 * @return Zone number or -1 on error
843
 */
845
 */
844
int zone_create(pfn_t start, count_t count, pfn_t confframe, int flags)
846
int zone_create(pfn_t start, count_t count, pfn_t confframe, int flags)
845
{
847
{
846
    zone_t *z;
848
    zone_t *z;
847
    uintptr_t addr;
849
    uintptr_t addr;
848
    count_t confcount;
850
    count_t confcount;
849
    int i;
851
    int i;
850
    int znum;
852
    int znum;
851
 
853
 
852
    /* Theoretically we could have here 0, practically make sure
854
    /* Theoretically we could have here 0, practically make sure
853
     * nobody tries to do that. If some platform requires, remove
855
     * nobody tries to do that. If some platform requires, remove
854
     * the assert
856
     * the assert
855
     */
857
     */
856
    ASSERT(confframe);
858
    ASSERT(confframe);
857
    /* If conframe is supposed to be inside our zone, then make sure
859
    /* If conframe is supposed to be inside our zone, then make sure
858
     * it does not span kernel & init
860
     * it does not span kernel & init
859
     */
861
     */
860
    confcount = SIZE2FRAMES(zone_conf_size(count));
862
    confcount = SIZE2FRAMES(zone_conf_size(count));
861
    if (confframe >= start && confframe < start+count) {
863
    if (confframe >= start && confframe < start+count) {
862
        for (;confframe < start + count; confframe++) {
864
        for (;confframe < start + count; confframe++) {
863
            addr = PFN2ADDR(confframe);
865
            addr = PFN2ADDR(confframe);
864
            if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.base), config.kernel_size))
866
            if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.base), config.kernel_size))
865
                continue;
867
                continue;
866
           
868
           
867
            if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.stack_base), config.stack_size))
869
            if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.stack_base), config.stack_size))
868
                continue;
870
                continue;
869
           
871
           
870
            bool overlap = false;
872
            bool overlap = false;
871
            count_t i;
873
            count_t i;
872
            for (i = 0; i < init.cnt; i++)
874
            for (i = 0; i < init.cnt; i++)
873
                if (overlaps(addr, PFN2ADDR(confcount), KA2PA(init.tasks[i].addr), init.tasks[i].size)) {
875
                if (overlaps(addr, PFN2ADDR(confcount), KA2PA(init.tasks[i].addr), init.tasks[i].size)) {
874
                    overlap = true;
876
                    overlap = true;
875
                    break;
877
                    break;
876
                }
878
                }
877
            if (overlap)
879
            if (overlap)
878
                continue;
880
                continue;
879
           
881
           
880
            break;
882
            break;
881
        }
883
        }
882
        if (confframe >= start + count)
884
        if (confframe >= start + count)
883
            panic("Cannot find configuration data for zone.");
885
            panic("Cannot find configuration data for zone.");
884
    }
886
    }
885
 
887
 
886
    z = (zone_t *)PA2KA(PFN2ADDR(confframe));
888
    z = (zone_t *)PA2KA(PFN2ADDR(confframe));
887
    zone_construct(start, count, z, flags);
889
    zone_construct(start, count, z, flags);
888
    znum = zones_add_zone(z);
890
    znum = zones_add_zone(z);
889
    if (znum == -1)
891
    if (znum == -1)
890
        return -1;
892
        return -1;
891
 
893
 
892
    /* If confdata in zone, mark as unavailable */
894
    /* If confdata in zone, mark as unavailable */
893
    if (confframe >= start && confframe < start+count)
895
    if (confframe >= start && confframe < start+count)
894
        for (i=confframe; i<confframe+confcount; i++) {
896
        for (i=confframe; i<confframe+confcount; i++) {
895
            zone_mark_unavailable(z, i - z->base);
897
            zone_mark_unavailable(z, i - z->base);
896
        }
898
        }
897
    return znum;
899
    return znum;
898
}
900
}
899
 
901
 
900
/***************************************/
902
/***************************************/
901
/* Frame functions */
903
/* Frame functions */
902
 
904
 
903
/** Set parent of frame */
905
/** Set parent of frame */
904
void frame_set_parent(pfn_t pfn, void *data, int hint)
906
void frame_set_parent(pfn_t pfn, void *data, int hint)
905
{
907
{
906
    zone_t *zone = find_zone_and_lock(pfn, &hint);
908
    zone_t *zone = find_zone_and_lock(pfn, &hint);
907
 
909
 
908
    ASSERT(zone);
910
    ASSERT(zone);
909
 
911
 
910
    zone_get_frame(zone, pfn-zone->base)->parent = data;
912
    zone_get_frame(zone, pfn-zone->base)->parent = data;
911
    spinlock_unlock(&zone->lock);
913
    spinlock_unlock(&zone->lock);
912
}
914
}
913
 
915
 
914
void * frame_get_parent(pfn_t pfn, int hint)
916
void * frame_get_parent(pfn_t pfn, int hint)
915
{
917
{
916
    zone_t *zone = find_zone_and_lock(pfn, &hint);
918
    zone_t *zone = find_zone_and_lock(pfn, &hint);
917
    void *res;
919
    void *res;
918
 
920
 
919
    ASSERT(zone);
921
    ASSERT(zone);
920
    res = zone_get_frame(zone, pfn - zone->base)->parent;
922
    res = zone_get_frame(zone, pfn - zone->base)->parent;
921
   
923
   
922
    spinlock_unlock(&zone->lock);
924
    spinlock_unlock(&zone->lock);
923
    return res;
925
    return res;
924
}
926
}
925
 
927
 
926
/** Allocate power-of-two frames of physical memory.
928
/** Allocate power-of-two frames of physical memory.
927
 *
929
 *
928
 * @param order  Allocate exactly 2^order frames.
930
 * @param order  Allocate exactly 2^order frames.
929
 * @param flags  Flags for host zone selection and address processing.
931
 * @param flags  Flags for host zone selection and address processing.
930
 * @param pzone  Preferred zone
932
 * @param pzone  Preferred zone
931
 *
933
 *
932
 * @return Physical address of the allocated frame.
934
 * @return Physical address of the allocated frame.
933
 *
935
 *
934
 */
936
 */
935
void * frame_alloc_generic(uint8_t order, int flags, int *pzone)
937
void * frame_alloc_generic(uint8_t order, int flags, int *pzone)
936
{
938
{
937
    ipl_t ipl;
939
    ipl_t ipl;
938
    int freed;
940
    int freed;
939
    pfn_t v;
941
    pfn_t v;
940
    zone_t *zone;
942
    zone_t *zone;
941
   
943
   
942
loop:
944
loop:
943
    ipl = interrupts_disable();
945
    ipl = interrupts_disable();
944
   
946
   
945
    /*
947
    /*
946
     * First, find suitable frame zone.
948
     * First, find suitable frame zone.
947
     */
949
     */
948
    zone = find_free_zone_and_lock(order, pzone);
950
    zone = find_free_zone_and_lock(order, pzone);
949
   
951
   
950
    /* If no memory, reclaim some slab memory,
952
    /* If no memory, reclaim some slab memory,
951
       if it does not help, reclaim all */
953
       if it does not help, reclaim all */
952
    if (!zone && !(flags & FRAME_NO_RECLAIM)) {
954
    if (!zone && !(flags & FRAME_NO_RECLAIM)) {
953
        freed = slab_reclaim(0);
955
        freed = slab_reclaim(0);
954
        if (freed)
956
        if (freed)
955
            zone = find_free_zone_and_lock(order, pzone);
957
            zone = find_free_zone_and_lock(order, pzone);
956
        if (!zone) {
958
        if (!zone) {
957
            freed = slab_reclaim(SLAB_RECLAIM_ALL);
959
            freed = slab_reclaim(SLAB_RECLAIM_ALL);
958
            if (freed)
960
            if (freed)
959
                zone = find_free_zone_and_lock(order, pzone);
961
                zone = find_free_zone_and_lock(order, pzone);
960
        }
962
        }
961
    }
963
    }
962
    if (!zone) {
964
    if (!zone) {
963
        /*
965
        /*
964
         * TODO: Sleep until frames are available again.
966
         * TODO: Sleep until frames are available again.
965
         */
967
         */
966
        interrupts_restore(ipl);
968
        interrupts_restore(ipl);
967
 
969
 
968
        if (flags & FRAME_ATOMIC)
970
        if (flags & FRAME_ATOMIC)
969
            return 0;
971
            return 0;
970
       
972
       
971
        panic("Sleep not implemented.\n");
973
        panic("Sleep not implemented.\n");
972
        goto loop;
974
        goto loop;
973
    }
975
    }
974
   
976
   
975
    v = zone_frame_alloc(zone, order);
977
    v = zone_frame_alloc(zone, order);
976
    v += zone->base;
978
    v += zone->base;
977
 
979
 
978
    spinlock_unlock(&zone->lock);
980
    spinlock_unlock(&zone->lock);
979
    interrupts_restore(ipl);
981
    interrupts_restore(ipl);
980
 
982
 
981
    if (flags & FRAME_KA)
983
    if (flags & FRAME_KA)
982
        return (void *)PA2KA(PFN2ADDR(v));
984
        return (void *)PA2KA(PFN2ADDR(v));
983
    return (void *)PFN2ADDR(v);
985
    return (void *)PFN2ADDR(v);
984
}
986
}
985
 
987
 
986
/** Free a frame.
988
/** Free a frame.
987
 *
989
 *
988
 * Find respective frame structure for supplied physical frame address.
990
 * Find respective frame structure for supplied physical frame address.
989
 * Decrement frame reference count.
991
 * Decrement frame reference count.
990
 * If it drops to zero, move the frame structure to free list.
992
 * If it drops to zero, move the frame structure to free list.
991
 *
993
 *
992
 * @param Frame Physical Address of of the frame to be freed.
994
 * @param Frame Physical Address of of the frame to be freed.
993
 */
995
 */
994
void frame_free(uintptr_t frame)
996
void frame_free(uintptr_t frame)
995
{
997
{
996
    ipl_t ipl;
998
    ipl_t ipl;
997
    zone_t *zone;
999
    zone_t *zone;
998
    pfn_t pfn = ADDR2PFN(frame);
1000
    pfn_t pfn = ADDR2PFN(frame);
999
 
1001
 
1000
    ipl = interrupts_disable();
1002
    ipl = interrupts_disable();
1001
   
1003
   
1002
    /*
1004
    /*
1003
     * First, find host frame zone for addr.
1005
     * First, find host frame zone for addr.
1004
     */
1006
     */
1005
    zone = find_zone_and_lock(pfn,NULL);
1007
    zone = find_zone_and_lock(pfn,NULL);
1006
    ASSERT(zone);
1008
    ASSERT(zone);
1007
   
1009
   
1008
    zone_frame_free(zone, pfn-zone->base);
1010
    zone_frame_free(zone, pfn-zone->base);
1009
   
1011
   
1010
    spinlock_unlock(&zone->lock);
1012
    spinlock_unlock(&zone->lock);
1011
    interrupts_restore(ipl);
1013
    interrupts_restore(ipl);
1012
}
1014
}
1013
 
1015
 
1014
/** Add reference to frame.
1016
/** Add reference to frame.
1015
 *
1017
 *
1016
 * Find respective frame structure for supplied PFN and
1018
 * Find respective frame structure for supplied PFN and
1017
 * increment frame reference count.
1019
 * increment frame reference count.
1018
 *
1020
 *
1019
 * @param pfn Frame number of the frame to be freed.
1021
 * @param pfn Frame number of the frame to be freed.
1020
 */
1022
 */
1021
void frame_reference_add(pfn_t pfn)
1023
void frame_reference_add(pfn_t pfn)
1022
{
1024
{
1023
    ipl_t ipl;
1025
    ipl_t ipl;
1024
    zone_t *zone;
1026
    zone_t *zone;
1025
    frame_t *frame;
1027
    frame_t *frame;
1026
 
1028
 
1027
    ipl = interrupts_disable();
1029
    ipl = interrupts_disable();
1028
   
1030
   
1029
    /*
1031
    /*
1030
     * First, find host frame zone for addr.
1032
     * First, find host frame zone for addr.
1031
     */
1033
     */
1032
    zone = find_zone_and_lock(pfn,NULL);
1034
    zone = find_zone_and_lock(pfn,NULL);
1033
    ASSERT(zone);
1035
    ASSERT(zone);
1034
   
1036
   
1035
    frame = &zone->frames[pfn-zone->base];
1037
    frame = &zone->frames[pfn-zone->base];
1036
    frame->refcount++;
1038
    frame->refcount++;
1037
   
1039
   
1038
    spinlock_unlock(&zone->lock);
1040
    spinlock_unlock(&zone->lock);
1039
    interrupts_restore(ipl);
1041
    interrupts_restore(ipl);
1040
}
1042
}
1041
 
1043
 
1042
/** Mark given range unavailable in frame zones */
1044
/** Mark given range unavailable in frame zones */
1043
void frame_mark_unavailable(pfn_t start, count_t count)
1045
void frame_mark_unavailable(pfn_t start, count_t count)
1044
{
1046
{
1045
    int i;
1047
    int i;
1046
    zone_t *zone;
1048
    zone_t *zone;
1047
    int prefzone = 0;
1049
    int prefzone = 0;
1048
   
1050
   
1049
    for (i=0; i < count; i++) {
1051
    for (i=0; i < count; i++) {
1050
        zone = find_zone_and_lock(start + i, &prefzone);
1052
        zone = find_zone_and_lock(start + i, &prefzone);
1051
        if (!zone) /* PFN not found */
1053
        if (!zone) /* PFN not found */
1052
            continue;
1054
            continue;
1053
        zone_mark_unavailable(zone, start + i - zone->base);
1055
        zone_mark_unavailable(zone, start + i - zone->base);
1054
 
1056
 
1055
        spinlock_unlock(&zone->lock);
1057
        spinlock_unlock(&zone->lock);
1056
    }
1058
    }
1057
}
1059
}
1058
 
1060
 
1059
/** Initialize physical memory management
1061
/** Initialize physical memory management
1060
 *
1062
 *
1061
 * Initialize physical memory managemnt.
1063
 * Initialize physical memory managemnt.
1062
 */
1064
 */
1063
void frame_init(void)
1065
void frame_init(void)
1064
{
1066
{
1065
    if (config.cpu_active == 1) {
1067
    if (config.cpu_active == 1) {
1066
        zones.count = 0;
1068
        zones.count = 0;
1067
        spinlock_initialize(&zones.lock, "zones.lock");
1069
        spinlock_initialize(&zones.lock, "zones.lock");
1068
    }
1070
    }
1069
    /* Tell the architecture to create some memory */
1071
    /* Tell the architecture to create some memory */
1070
    frame_arch_init();
1072
    frame_arch_init();
1071
    if (config.cpu_active == 1) {
1073
    if (config.cpu_active == 1) {
1072
        frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)), SIZE2FRAMES(config.kernel_size));
1074
        frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)), SIZE2FRAMES(config.kernel_size));
1073
        frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)), SIZE2FRAMES(config.stack_size));
1075
        frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)), SIZE2FRAMES(config.stack_size));
1074
       
1076
       
1075
        count_t i;
1077
        count_t i;
1076
        for (i = 0; i < init.cnt; i++)
1078
        for (i = 0; i < init.cnt; i++)
1077
            frame_mark_unavailable(ADDR2PFN(KA2PA(init.tasks[i].addr)), SIZE2FRAMES(init.tasks[i].size));
1079
            frame_mark_unavailable(ADDR2PFN(KA2PA(init.tasks[i].addr)), SIZE2FRAMES(init.tasks[i].size));
1078
 
1080
 
1079
        if (ballocs.size)
1081
        if (ballocs.size)
1080
            frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)), SIZE2FRAMES(ballocs.size));
1082
            frame_mark_unavailable(ADDR2PFN(KA2PA(ballocs.base)), SIZE2FRAMES(ballocs.size));
1081
 
1083
 
1082
        /* Black list first frame, as allocating NULL would
1084
        /* Black list first frame, as allocating NULL would
1083
         * fail in some places */
1085
         * fail in some places */
1084
        frame_mark_unavailable(0, 1);
1086
        frame_mark_unavailable(0, 1);
1085
    }
1087
    }
1086
}
1088
}
1087
 
1089
 
1088
 
1090
 
1089
 
1091
 
1090
/** Prints list of zones
1092
/** Prints list of zones
1091
 *
1093
 *
1092
 */
1094
 */
1093
void zone_print_list(void) {
1095
void zone_print_list(void) {
1094
    zone_t *zone = NULL;
1096
    zone_t *zone = NULL;
1095
    int i;
1097
    int i;
1096
    ipl_t ipl;
1098
    ipl_t ipl;
1097
 
1099
 
1098
    ipl = interrupts_disable();
1100
    ipl = interrupts_disable();
1099
    spinlock_lock(&zones.lock);
1101
    spinlock_lock(&zones.lock);
1100
    printf("#  base address free frames  busy frames\n");
1102
    printf("#  base address free frames  busy frames\n");
1101
    printf("-- ------------ ------------ ------------\n");
1103
    printf("-- ------------ ------------ ------------\n");
1102
    for (i = 0; i < zones.count; i++) {
1104
    for (i = 0; i < zones.count; i++) {
1103
        zone = zones.info[i];
1105
        zone = zones.info[i];
1104
        spinlock_lock(&zone->lock);
1106
        spinlock_lock(&zone->lock);
1105
        printf("%-2d %12p %12zd %12zd\n", i, PFN2ADDR(zone->base), zone->free_count, zone->busy_count);
1107
        printf("%-2d %12p %12zd %12zd\n", i, PFN2ADDR(zone->base), zone->free_count, zone->busy_count);
1106
        spinlock_unlock(&zone->lock);
1108
        spinlock_unlock(&zone->lock);
1107
    }
1109
    }
1108
    spinlock_unlock(&zones.lock);
1110
    spinlock_unlock(&zones.lock);
1109
    interrupts_restore(ipl);
1111
    interrupts_restore(ipl);
1110
}
1112
}
1111
 
1113
 
1112
/** Prints zone details.
1114
/** Prints zone details.
1113
 *
1115
 *
1114
 * @param num Zone base address or zone number.
1116
 * @param num Zone base address or zone number.
1115
 */
1117
 */
1116
void zone_print_one(int num) {
1118
void zone_print_one(int num) {
1117
    zone_t *zone = NULL;
1119
    zone_t *zone = NULL;
1118
    ipl_t ipl;
1120
    ipl_t ipl;
1119
    int i;
1121
    int i;
1120
 
1122
 
1121
    ipl = interrupts_disable();
1123
    ipl = interrupts_disable();
1122
    spinlock_lock(&zones.lock);
1124
    spinlock_lock(&zones.lock);
1123
 
1125
 
1124
    for (i = 0; i < zones.count; i++) {
1126
    for (i = 0; i < zones.count; i++) {
1125
        if (i == num || PFN2ADDR(zones.info[i]->base) == num) {
1127
        if (i == num || PFN2ADDR(zones.info[i]->base) == num) {
1126
            zone = zones.info[i];
1128
            zone = zones.info[i];
1127
            break;
1129
            break;
1128
        }
1130
        }
1129
    }
1131
    }
1130
    if (!zone) {
1132
    if (!zone) {
1131
        printf("Zone not found.\n");
1133
        printf("Zone not found.\n");
1132
        goto out;
1134
        goto out;
1133
    }
1135
    }
1134
   
1136
   
1135
    spinlock_lock(&zone->lock);
1137
    spinlock_lock(&zone->lock);
1136
    printf("Memory zone information\n");
1138
    printf("Memory zone information\n");
1137
    printf("Zone base address: %#.*p\n", sizeof(uintptr_t) * 2, PFN2ADDR(zone->base));
1139
    printf("Zone base address: %#.*p\n", sizeof(uintptr_t) * 2, PFN2ADDR(zone->base));
1138
    printf("Zone size: %zd frames (%zdK)\n", zone->count, ((zone->count) * FRAME_SIZE) >> 10);
1140
    printf("Zone size: %zd frames (%zdK)\n", zone->count, ((zone->count) * FRAME_SIZE) >> 10);
1139
    printf("Allocated space: %zd frames (%zdK)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10);
1141
    printf("Allocated space: %zd frames (%zdK)\n", zone->busy_count, (zone->busy_count * FRAME_SIZE) >> 10);
1140
    printf("Available space: %zd frames (%zdK)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10);
1142
    printf("Available space: %zd frames (%zdK)\n", zone->free_count, (zone->free_count * FRAME_SIZE) >> 10);
1141
    buddy_system_structure_print(zone->buddy_system, FRAME_SIZE);
1143
    buddy_system_structure_print(zone->buddy_system, FRAME_SIZE);
1142
   
1144
   
1143
    spinlock_unlock(&zone->lock);
1145
    spinlock_unlock(&zone->lock);
1144
out:
1146
out:
1145
    spinlock_unlock(&zones.lock);
1147
    spinlock_unlock(&zones.lock);
1146
    interrupts_restore(ipl);
1148
    interrupts_restore(ipl);
1147
}
1149
}
1148
 
1150
 
1149
/** @}
1151
/** @}
1150
 */
1152
 */
1151
 
1153