Subversion Repositories HelenOS-historic

Rev

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

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