Subversion Repositories HelenOS-historic

Rev

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

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