Subversion Repositories HelenOS-historic

Rev

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

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