Subversion Repositories HelenOS-historic

Rev

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

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