Subversion Repositories HelenOS-historic

Rev

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

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