Subversion Repositories HelenOS

Rev

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

Rev 368 Rev 373
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <typedefs.h>
29
#include <typedefs.h>
30
#include <arch/types.h>
30
#include <arch/types.h>
31
#include <mm/heap.h>
31
#include <mm/heap.h>
32
#include <mm/frame.h>
32
#include <mm/frame.h>
33
#include <mm/vm.h>
33
#include <mm/vm.h>
34
#include <panic.h>
34
#include <panic.h>
35
#include <debug.h>
35
#include <debug.h>
36
#include <list.h>
36
#include <list.h>
37
#include <synch/spinlock.h>
37
#include <synch/spinlock.h>
38
#include <arch/asm.h>
38
#include <arch/asm.h>
39
#include <arch.h>
39
#include <arch.h>
40
 
40
 
41
spinlock_t zone_head_lock;       /**< this lock protects zone_head list */
41
spinlock_t zone_head_lock;       /**< this lock protects zone_head list */
42
link_t zone_head;                /**< list of all zones in the system */
42
link_t zone_head;                /**< list of all zones in the system */
43
 
43
 
44
/** Initialize physical memory management
44
/** Initialize physical memory management
45
 *
45
 *
46
 * Initialize physical memory managemnt.
46
 * Initialize physical memory managemnt.
47
 */
47
 */
48
void frame_init(void)
48
void frame_init(void)
49
{
49
{
50
    if (config.cpu_active == 1) {
50
    if (config.cpu_active == 1) {
51
        zone_init();
51
        zone_init();
52
    }
52
    }
53
 
53
 
54
    frame_arch_init();
54
    frame_arch_init();
55
   
55
   
56
    if (config.cpu_active == 1) {
56
    if (config.cpu_active == 1) {
57
                frame_region_not_free(config.base, config.base + config.kernel_size + CONFIG_STACK_SIZE);
57
                frame_region_not_free(config.base, config.base + config.kernel_size + CONFIG_STACK_SIZE);
58
        }  
58
        }  
59
}
59
}
60
 
60
 
61
/** Allocate a frame
61
/** Allocate a frame
62
 *
62
 *
63
 * Allocate a frame of physical memory.
63
 * Allocate a frame of physical memory.
64
 *
64
 *
65
 * @param flags Flags for host zone selection and address processing.
65
 * @param flags Flags for host zone selection and address processing.
66
 *
66
 *
67
 * @return Allocated frame.
67
 * @return Allocated frame.
68
 */
68
 */
69
__address frame_alloc(int flags)
69
__address frame_alloc(int flags)
70
{
70
{
71
    pri_t pri;
71
    pri_t pri;
72
    link_t *cur, *tmp;
72
    link_t *cur, *tmp;
73
    zone_t *z;
73
    zone_t *z;
74
    zone_t *zone = NULL;
74
    zone_t *zone = NULL;
75
    frame_t *frame = NULL;
75
    frame_t *frame = NULL;
76
    __address v;
76
    __address v;
77
   
77
   
78
loop:
78
loop:
79
    pri = cpu_priority_high();
79
    pri = cpu_priority_high();
80
    spinlock_lock(&zone_head_lock);
80
    spinlock_lock(&zone_head_lock);
81
   
81
   
82
    /*
82
    /*
83
     * First, find suitable frame zone.
83
     * First, find suitable frame zone.
84
     */
84
     */
85
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
85
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
86
        z = list_get_instance(cur, zone_t, link);
86
        z = list_get_instance(cur, zone_t, link);
87
       
87
       
88
        spinlock_lock(&z->lock);
88
        spinlock_lock(&z->lock);
89
        /*
89
        /*
90
         * Check if the zone has any free frames.
90
         * Check if the zone has any free frames.
91
         */
91
         */
92
        if (z->free_count) {
92
        if (z->free_count) {
93
            zone = z;
93
            zone = z;
94
            break;
94
            break;
95
        }
95
        }
96
        spinlock_unlock(&z->lock);
96
        spinlock_unlock(&z->lock);
97
    }
97
    }
98
   
98
   
99
    if (!zone) {
99
    if (!zone) {
100
        if (flags & FRAME_PANIC)
100
        if (flags & FRAME_PANIC)
101
            panic("Can't allocate frame.\n");
101
            panic("Can't allocate frame.\n");
102
       
102
       
103
        /*
103
        /*
104
         * TODO: Sleep until frames are available again.
104
         * TODO: Sleep until frames are available again.
105
         */
105
         */
106
        spinlock_unlock(&zone_head_lock);
106
        spinlock_unlock(&zone_head_lock);
107
        cpu_priority_restore(pri);
107
        cpu_priority_restore(pri);
108
 
108
 
109
        panic("Sleep not implemented.\n");
109
        panic("Sleep not implemented.\n");
110
        goto loop;
110
        goto loop;
111
    }
111
    }
112
       
112
       
113
    tmp = zone->free_head.next;
113
    tmp = zone->free_head.next;
114
    frame = list_get_instance(tmp, frame_t, link);
114
    frame = list_get_instance(tmp, frame_t, link);
115
 
115
 
116
    frame->refcount++;
116
    frame->refcount++;
117
    list_remove(tmp);           /* remove frame from free_head */
117
    list_remove(tmp);           /* remove frame from free_head */
118
    list_append(tmp, &zone->busy_head); /* append frame to busy_head */
-
 
119
    zone->free_count--;
118
    zone->free_count--;
120
    zone->busy_count++;
119
    zone->busy_count++;
121
   
120
   
122
    v = zone->base + (frame - zone->frames) * FRAME_SIZE;
121
    v = zone->base + (frame - zone->frames) * FRAME_SIZE;
123
   
122
   
124
    if (flags & FRAME_KA)
123
    if (flags & FRAME_KA)
125
        v = PA2KA(v);
124
        v = PA2KA(v);
126
   
125
   
127
    spinlock_unlock(&zone->lock);
126
    spinlock_unlock(&zone->lock);
128
   
127
   
129
    spinlock_unlock(&zone_head_lock);
128
    spinlock_unlock(&zone_head_lock);
130
    cpu_priority_restore(pri);
129
    cpu_priority_restore(pri);
131
   
130
   
132
    return v;
131
    return v;
133
}
132
}
134
 
133
 
135
/** Free a frame.
134
/** Free a frame.
136
 *
135
 *
137
 * Find respective frame structrue for supplied addr.
136
 * Find respective frame structrue for supplied addr.
138
 * Decrement frame reference count.
137
 * Decrement frame reference count.
139
 * If it drops to zero, move the frame structure to free list.
138
 * If it drops to zero, move the frame structure to free list.
140
 *
139
 *
141
 * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE.
140
 * @param addr Address of the frame to be freed. It must be a multiple of FRAME_SIZE.
142
 */
141
 */
143
void frame_free(__address addr)
142
void frame_free(__address addr)
144
{
143
{
145
    pri_t pri;
144
    pri_t pri;
146
    link_t *cur;
145
    link_t *cur;
147
    zone_t *z;
146
    zone_t *z;
148
    zone_t *zone = NULL;
147
    zone_t *zone = NULL;
149
    frame_t *frame;
148
    frame_t *frame;
150
   
149
   
151
    ASSERT(addr % FRAME_SIZE == 0);
150
    ASSERT(addr % FRAME_SIZE == 0);
152
   
151
   
153
    pri = cpu_priority_high();
152
    pri = cpu_priority_high();
154
    spinlock_lock(&zone_head_lock);
153
    spinlock_lock(&zone_head_lock);
155
   
154
   
156
    /*
155
    /*
157
     * First, find host frame zone for addr.
156
     * First, find host frame zone for addr.
158
     */
157
     */
159
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
158
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
160
        z = list_get_instance(cur, zone_t, link);
159
        z = list_get_instance(cur, zone_t, link);
161
       
160
       
162
        spinlock_lock(&z->lock);
161
        spinlock_lock(&z->lock);
163
       
162
       
164
        if (IS_KA(addr))
163
        if (IS_KA(addr))
165
            addr = KA2PA(addr);
164
            addr = KA2PA(addr);
166
       
165
       
167
        /*
166
        /*
168
         * Check if addr belongs to z.
167
         * Check if addr belongs to z.
169
         */
168
         */
170
        if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
169
        if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
171
            zone = z;
170
            zone = z;
172
            break;
171
            break;
173
        }
172
        }
174
        spinlock_unlock(&z->lock);
173
        spinlock_unlock(&z->lock);
175
    }
174
    }
176
   
175
   
177
    ASSERT(zone != NULL);
176
    ASSERT(zone != NULL);
178
   
177
   
179
    frame = &zone->frames[(addr - zone->base)/FRAME_SIZE];
178
    frame = &zone->frames[(addr - zone->base)/FRAME_SIZE];
180
    ASSERT(frame->refcount);
179
    ASSERT(frame->refcount);
181
 
180
 
182
    if (!--frame->refcount) {
181
    if (!--frame->refcount) {
183
        list_remove(&frame->link);          /* remove frame from busy_head */
-
 
184
        list_append(&frame->link, &zone->free_head);    /* append frame to free_head */
182
        list_append(&frame->link, &zone->free_head);    /* append frame to free_head */
185
        zone->free_count++;
183
        zone->free_count++;
186
        zone->busy_count--;
184
        zone->busy_count--;
187
    }
185
    }
188
   
186
   
189
    spinlock_unlock(&zone->lock);  
187
    spinlock_unlock(&zone->lock);  
190
   
188
   
191
    spinlock_unlock(&zone_head_lock);
189
    spinlock_unlock(&zone_head_lock);
192
    cpu_priority_restore(pri);
190
    cpu_priority_restore(pri);
193
}
191
}
194
 
192
 
195
/** Mark frame not free.
193
/** Mark frame not free.
196
 *
194
 *
197
 * Find respective frame structrue for supplied addr.
195
 * Find respective frame structrue for supplied addr.
198
 * Increment frame reference count and move the frame structure to busy list.
196
 * Increment frame reference count and remove the frame structure from free list.
199
 *
197
 *
200
 * @param addr Address of the frame to be marked. It must be a multiple of FRAME_SIZE.
198
 * @param addr Address of the frame to be marked. It must be a multiple of FRAME_SIZE.
201
 */
199
 */
202
void frame_not_free(__address addr)
200
void frame_not_free(__address addr)
203
{
201
{
204
    pri_t pri;
202
    pri_t pri;
205
    link_t *cur;
203
    link_t *cur;
206
    zone_t *z;
204
    zone_t *z;
207
    zone_t *zone = NULL;
205
    zone_t *zone = NULL;
208
    frame_t *frame;
206
    frame_t *frame;
209
   
207
   
210
    ASSERT(addr % FRAME_SIZE == 0);
208
    ASSERT(addr % FRAME_SIZE == 0);
211
   
209
   
212
    pri = cpu_priority_high();
210
    pri = cpu_priority_high();
213
    spinlock_lock(&zone_head_lock);
211
    spinlock_lock(&zone_head_lock);
214
   
212
   
215
    /*
213
    /*
216
     * First, find host frame zone for addr.
214
     * First, find host frame zone for addr.
217
     */
215
     */
218
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
216
    for (cur = zone_head.next; cur != &zone_head; cur = cur->next) {
219
        z = list_get_instance(cur, zone_t, link);
217
        z = list_get_instance(cur, zone_t, link);
220
       
218
       
221
        spinlock_lock(&z->lock);
219
        spinlock_lock(&z->lock);
222
       
220
       
223
        if (IS_KA(addr))
221
        if (IS_KA(addr))
224
            addr = KA2PA(addr);
222
            addr = KA2PA(addr);
225
       
223
       
226
        /*
224
        /*
227
         * Check if addr belongs to z.
225
         * Check if addr belongs to z.
228
         */
226
         */
229
        if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
227
        if ((addr >= z->base) && (addr <= z->base + (z->free_count + z->busy_count) * FRAME_SIZE)) {
230
            zone = z;
228
            zone = z;
231
            break;
229
            break;
232
        }
230
        }
233
        spinlock_unlock(&z->lock);
231
        spinlock_unlock(&z->lock);
234
    }
232
    }
235
   
233
   
236
    ASSERT(zone != NULL);
234
    ASSERT(zone != NULL);
237
   
235
   
238
    frame = &zone->frames[(addr - zone->base)/FRAME_SIZE];
236
    frame = &zone->frames[(addr - zone->base)/FRAME_SIZE];
239
 
237
 
240
    if (!frame->refcount) {
238
    if (!frame->refcount) {
241
        frame->refcount++;
239
        frame->refcount++;
242
 
240
 
243
        list_remove(&frame->link);          /* remove frame from free_head */
241
        list_remove(&frame->link);          /* remove frame from free_head */
244
        list_append(&frame->link, &zone->busy_head);    /* append frame to busy_head */
-
 
245
        zone->free_count--;
242
        zone->free_count--;
246
        zone->busy_count++;
243
        zone->busy_count++;
247
    }
244
    }
248
   
245
   
249
    spinlock_unlock(&zone->lock);  
246
    spinlock_unlock(&zone->lock);  
250
   
247
   
251
    spinlock_unlock(&zone_head_lock);
248
    spinlock_unlock(&zone_head_lock);
252
    cpu_priority_restore(pri);
249
    cpu_priority_restore(pri);
253
}
250
}
254
 
251
 
255
/** Mark frame region not free.
252
/** Mark frame region not free.
256
 *
253
 *
257
 * Mark frame region not free.
254
 * Mark frame region not free.
258
 *
255
 *
259
 * @param start First address.
256
 * @param start First address.
260
 * @param stop Last address.
257
 * @param stop Last address.
261
 */
258
 */
262
void frame_region_not_free(__address start, __address stop)
259
void frame_region_not_free(__address start, __address stop)
263
{
260
{
264
        __address a;
261
        __address a;
265
 
262
 
266
        start /= FRAME_SIZE;
263
        start /= FRAME_SIZE;
267
        stop /= FRAME_SIZE;
264
        stop /= FRAME_SIZE;
268
        for (a = start; a <= stop; a++)
265
        for (a = start; a <= stop; a++)
269
                frame_not_free(a * FRAME_SIZE);
266
                frame_not_free(a * FRAME_SIZE);
270
}
267
}
271
 
268
 
272
 
269
 
273
/** Initialize zonekeeping
270
/** Initialize zonekeeping
274
 *
271
 *
275
 * Initialize zonekeeping.
272
 * Initialize zonekeeping.
276
 */
273
 */
277
void zone_init(void)
274
void zone_init(void)
278
{
275
{
279
    spinlock_initialize(&zone_head_lock);
276
    spinlock_initialize(&zone_head_lock);
280
    list_initialize(&zone_head);
277
    list_initialize(&zone_head);
281
}
278
}
282
 
279
 
283
/** Create frame zone
280
/** Create frame zone
284
 *
281
 *
285
 * Create new frame zone.
282
 * Create new frame zone.
286
 *
283
 *
287
 * @param start Physical address of the first frame within the zone.
284
 * @param start Physical address of the first frame within the zone.
288
 * @param size Size of the zone. Must be a multiple of FRAME_SIZE.
285
 * @param size Size of the zone. Must be a multiple of FRAME_SIZE.
289
 * @param flags Zone flags.
286
 * @param flags Zone flags.
290
 *
287
 *
291
 * @return Initialized zone.
288
 * @return Initialized zone.
292
 */
289
 */
293
zone_t *zone_create(__address start, size_t size, int flags)
290
zone_t *zone_create(__address start, size_t size, int flags)
294
{
291
{
295
    zone_t *z;
292
    zone_t *z;
296
    count_t cnt;
293
    count_t cnt;
297
    int i;
294
    int i;
298
   
295
   
299
    ASSERT(start % FRAME_SIZE == 0);
296
    ASSERT(start % FRAME_SIZE == 0);
300
    ASSERT(size % FRAME_SIZE == 0);
297
    ASSERT(size % FRAME_SIZE == 0);
301
   
298
   
302
    cnt = size / FRAME_SIZE;
299
    cnt = size / FRAME_SIZE;
303
   
300
   
304
    z = (zone_t *) malloc(sizeof(zone_t));
301
    z = (zone_t *) malloc(sizeof(zone_t));
305
    if (z) {
302
    if (z) {
306
        link_initialize(&z->link);
303
        link_initialize(&z->link);
307
        spinlock_initialize(&z->lock);
304
        spinlock_initialize(&z->lock);
308
   
305
   
309
        z->base = start;
306
        z->base = start;
310
        z->flags = flags;
307
        z->flags = flags;
311
 
308
 
312
        z->free_count = cnt;
309
        z->free_count = cnt;
313
        list_initialize(&z->free_head);
310
        list_initialize(&z->free_head);
314
 
311
 
315
        z->busy_count = 0;
312
        z->busy_count = 0;
316
        list_initialize(&z->busy_head);
-
 
317
       
313
       
318
        z->frames = (frame_t *) malloc(cnt * sizeof(frame_t));
314
        z->frames = (frame_t *) malloc(cnt * sizeof(frame_t));
319
        if (!z->frames) {
315
        if (!z->frames) {
320
            free(z);
316
            free(z);
321
            return NULL;
317
            return NULL;
322
        }
318
        }
323
       
319
       
324
        for (i = 0; i<cnt; i++) {
320
        for (i = 0; i<cnt; i++) {
325
            frame_initialize(&z->frames[i], z);
321
            frame_initialize(&z->frames[i], z);
326
            list_append(&z->frames[i].link, &z->free_head);
322
            list_append(&z->frames[i].link, &z->free_head);
327
        }
323
        }
328
       
324
       
329
    }
325
    }
330
   
326
   
331
    return z;
327
    return z;
332
}
328
}
333
 
329
 
334
/** Attach frame zone
330
/** Attach frame zone
335
 *
331
 *
336
 * Attach frame zone to zone list.
332
 * Attach frame zone to zone list.
337
 *
333
 *
338
 * @param zone Zone to be attached.
334
 * @param zone Zone to be attached.
339
 */
335
 */
340
void zone_attach(zone_t *zone)
336
void zone_attach(zone_t *zone)
341
{
337
{
342
    pri_t pri;
338
    pri_t pri;
343
   
339
   
344
    pri = cpu_priority_high();
340
    pri = cpu_priority_high();
345
    spinlock_lock(&zone_head_lock);
341
    spinlock_lock(&zone_head_lock);
346
   
342
   
347
    list_append(&zone->link, &zone_head);
343
    list_append(&zone->link, &zone_head);
348
   
344
   
349
    spinlock_unlock(&zone_head_lock);
345
    spinlock_unlock(&zone_head_lock);
350
    cpu_priority_restore(pri);
346
    cpu_priority_restore(pri);
351
}
347
}
352
 
348
 
353
/** Initialize frame structure
349
/** Initialize frame structure
354
 *
350
 *
355
 * Initialize frame structure.
351
 * Initialize frame structure.
356
 *
352
 *
357
 * @param frame Frame structure to be initialized.
353
 * @param frame Frame structure to be initialized.
358
 * @param zone Host frame zone.
354
 * @param zone Host frame zone.
359
 */
355
 */
360
void frame_initialize(frame_t *frame, zone_t *zone)
356
void frame_initialize(frame_t *frame, zone_t *zone)
361
{
357
{
362
    frame->refcount = 0;
358
    frame->refcount = 0;
363
    link_initialize(&frame->link);
359
    link_initialize(&frame->link);
364
}
360
}
365
 
361