Subversion Repositories HelenOS-historic

Rev

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

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