Subversion Repositories HelenOS

Rev

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

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