Subversion Repositories HelenOS-historic

Rev

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

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