Subversion Repositories HelenOS

Rev

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

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