Subversion Repositories HelenOS

Rev

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

Rev 68 Rev 102
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 <arch/types.h>
29
#include <arch/types.h>
30
#include <func.h>
30
#include <func.h>
31
 
31
 
32
#include <mm/heap.h>
32
#include <mm/heap.h>
33
#include <mm/frame.h>
33
#include <mm/frame.h>
34
#include <mm/page.h>
34
#include <mm/page.h>
35
#include <mm/vm.h>
35
#include <mm/vm.h>
36
#include <arch/mm/page.h>
36
#include <arch/mm/page.h>
37
 
37
 
38
#include <config.h>
38
#include <config.h>
39
#include <memstr.h>
39
#include <memstr.h>
40
 
40
 
41
#include <panic.h>
41
#include <panic.h>
42
 
42
 
43
#include <synch/spinlock.h>
43
#include <synch/spinlock.h>
44
 
44
 
45
__u32 frames;
45
count_t frames = 0;
46
__u32 frames_free;
46
count_t frames_free;
47
 
47
 
48
__u8 *frame_bitmap;
48
__u8 *frame_bitmap;
49
__u32 frame_bitmap_octets;
49
count_t frame_bitmap_octets;
50
 
50
 
51
/*
51
/*
52
 * This is for kernel address space frames (allocated with FRAME_KA).
52
 * This is for kernel address space frames (allocated with FRAME_KA).
53
 * Their addresses may not interfere with user address space.
53
 * Their addresses may not interfere with user address space.
54
 */
54
 */
55
__u8 *frame_kernel_bitmap;
55
__u8 *frame_kernel_bitmap;
56
__u32 kernel_frames;
56
count_t kernel_frames;
57
__u32 kernel_frames_free;
57
count_t kernel_frames_free;
58
 
58
 
59
static spinlock_t framelock;
59
static spinlock_t framelock;
60
 
60
 
61
void frame_init(void)
61
void frame_init(void)
62
{
62
{
63
        if (config.cpu_active == 1) {
63
        if (config.cpu_active == 1) {
64
 
64
 
65
                /*
65
                /*
66
                 * The bootstrap processor will allocate all necessary memory for frame allocation.
66
                 * The bootstrap processor will allocate all necessary memory for frame allocation.
67
                 */
67
                 */
68
 
68
 
69
                frames = config.memory_size / FRAME_SIZE;
69
                frames = config.memory_size / FRAME_SIZE;
70
                frame_bitmap_octets = frames / 8 + (frames % 8 > 0);
70
                frame_bitmap_octets = frames / 8 + (frames % 8 > 0);
71
 
-
 
72
                frame_bitmap = (__u8 *) malloc(frame_bitmap_octets);
71
                frame_bitmap = (__u8 *) malloc(frame_bitmap_octets);
73
                if (!frame_bitmap)
72
                if (!frame_bitmap)
74
                        panic("malloc/frame_bitmap\n");
73
                        panic("malloc/frame_bitmap\n");
75
 
74
 
76
                /*
75
                /*
77
                 * Mark all frames free.
76
                 * Mark all frames free.
78
                 */
77
                 */
79
                memsetb((__address) frame_bitmap, frame_bitmap_octets, 0);
78
                memsetb((__address) frame_bitmap, frame_bitmap_octets, 0);
80
                frames_free = frames;
79
                frames_free = frames;
81
 
80
 
82
        /*
81
        /*
83
         * Will be properly set up by architecture dependent frame init.
82
         * Will be properly set up by architecture dependent frame init.
84
         */
83
         */
85
        frame_kernel_bitmap = NULL;
84
        frame_kernel_bitmap = NULL;
86
        kernel_frames_free = 0;
85
        kernel_frames_free = 0;
87
        kernel_frames = 0; 
86
        kernel_frames = 0; 
88
    }
87
    }
89
 
88
 
90
    /*
89
    /*
91
     * No frame allocations/reservations  prior this point.
90
     * No frame allocations/reservations  prior this point.
92
     */
91
     */
93
 
92
 
94
    frame_arch_init();
93
    frame_arch_init();
95
 
94
 
96
    if (config.cpu_active == 1) {
95
    if (config.cpu_active == 1) {
97
                /*
96
                /*
98
                 * Create the memory address space map. Marked frames and frame
97
                 * Create the memory address space map. Marked frames and frame
99
                 * regions cannot be used for allocation.
98
                 * regions cannot be used for allocation.
100
                 */
99
                 */
101
        frame_region_not_free(config.base, config.base + config.kernel_size);
100
        frame_region_not_free(config.base, config.base + config.kernel_size);
102
    }
101
    }
103
}
102
}
104
 
103
 
105
/*
104
/*
106
 * Allocate a frame.
105
 * Allocate a frame.
107
 */
106
 */
108
__address frame_alloc(int flags)
107
__address frame_alloc(int flags)
109
{
108
{
110
    int i;
109
    int i;
111
    pri_t pri;
110
    pri_t pri;
112
    __u8 **frame_bitmap_ptr = &frame_bitmap;
111
    __u8 **frame_bitmap_ptr = &frame_bitmap;
113
    __u32 *frames_ptr = &frames, *frames_free_ptr = &frames_free;
112
    count_t *frames_ptr = &frames, *frames_free_ptr = &frames_free;
114
   
113
   
115
    if (flags & FRAME_KA) {
114
    if (flags & FRAME_KA) {
116
        frame_bitmap_ptr = &frame_kernel_bitmap;
115
        frame_bitmap_ptr = &frame_kernel_bitmap;
117
        frames_ptr = &kernel_frames;
116
        frames_ptr = &kernel_frames;
118
        frames_free_ptr = &kernel_frames_free;
117
        frames_free_ptr = &kernel_frames_free;
119
    }
118
    }
120
   
119
   
121
loop:
120
loop:
122
    pri = cpu_priority_high();
121
    pri = cpu_priority_high();
123
    spinlock_lock(&framelock);
122
    spinlock_lock(&framelock);
124
    if (*frames_free_ptr) {
123
    if (*frames_free_ptr) {
125
        for (i=0; i < *frames_ptr; i++) {
124
        for (i=0; i < *frames_ptr; i++) {
126
            int m, n;
125
            int m, n;
127
       
126
       
128
            m = i / 8;
127
            m = i / 8;
129
            n = i % 8;
128
            n = i % 8;
130
 
129
 
131
            if (((*frame_bitmap_ptr)[m] & (1<<n)) == 0) {
130
            if (((*frame_bitmap_ptr)[m] & (1<<n)) == 0) {
132
                (*frame_bitmap_ptr)[m] |= (1<<n);
131
                (*frame_bitmap_ptr)[m] |= (1<<n);
133
                *frames_free_ptr--;
132
                *frames_free_ptr--;
134
                if (flags & FRAME_KA) {
133
                if (flags & FRAME_KA) {
135
                    /*
134
                    /*
136
                     * frames_free_ptr points to kernel_frames_free
135
                     * frames_free_ptr points to kernel_frames_free
137
                     * It is still necessary to decrement frames_free.
136
                     * It is still necessary to decrement frames_free.
138
                     */
137
                     */
139
                    frames_free--;
138
                    frames_free--;
140
                }
139
                }
141
                spinlock_unlock(&framelock);
140
                spinlock_unlock(&framelock);
142
                cpu_priority_restore(pri);
141
                cpu_priority_restore(pri);
143
                if (flags & FRAME_KA) return PA2KA(i*FRAME_SIZE);
142
                if (flags & FRAME_KA) return PA2KA(i*FRAME_SIZE);
144
                return i*FRAME_SIZE;
143
                return i*FRAME_SIZE;
145
            }
144
            }
146
        }
145
        }
147
        panic("frames_free inconsistent (%d)\n", frames_free);
146
        panic("frames_free inconsistent (%d)\n", frames_free);
148
    }
147
    }
149
    spinlock_unlock(&framelock);
148
    spinlock_unlock(&framelock);
150
    cpu_priority_restore(pri);
149
    cpu_priority_restore(pri);
151
 
150
 
152
    if (flags & FRAME_PANIC)
151
    if (flags & FRAME_PANIC)
153
        panic("unable to allocate frame\n");
152
        panic("unable to allocate frame\n");
154
       
153
       
155
    /* TODO: implement sleeping logic here */
154
    /* TODO: implement sleeping logic here */
156
    panic("sleep not supported\n");
155
    panic("sleep not supported\n");
157
   
156
   
158
    goto loop;
157
    goto loop;
159
}
158
}
160
 
159
 
161
/*
160
/*
162
 * Free a frame.
161
 * Free a frame.
163
 */
162
 */
164
void frame_free(__address addr)
163
void frame_free(__address addr)
165
{
164
{
166
    pri_t pri;
165
    pri_t pri;
167
    __u32 frame;
166
    __u32 frame;
168
    __u32 *frames_free_ptr = &frames_free, *frames_ptr = &frames;
167
    count_t *frames_free_ptr = &frames_free, *frames_ptr = &frames;
169
    __u8 **frame_bitmap_ptr = &frame_bitmap;
168
    __u8 **frame_bitmap_ptr = &frame_bitmap;
170
 
169
 
171
    if (IS_KA(addr)) {
170
    if (IS_KA(addr)) {
172
        frames_free_ptr = &kernel_frames_free;
171
        frames_free_ptr = &kernel_frames_free;
173
        frame_bitmap_ptr = &frame_kernel_bitmap;
172
        frame_bitmap_ptr = &frame_kernel_bitmap;
174
    }
173
    }
175
 
174
 
176
    pri = cpu_priority_high();
175
    pri = cpu_priority_high();
177
    spinlock_lock(&framelock);
176
    spinlock_lock(&framelock);
178
   
177
   
179
    frame = IS_KA(addr) ? KA2PA(addr) : addr;
178
    frame = IS_KA(addr) ? KA2PA(addr) : addr;
180
    frame /= FRAME_SIZE;
179
    frame /= FRAME_SIZE;
181
    if (frame < *frames_ptr) {
180
    if (frame < *frames_ptr) {
182
        int m, n;
181
        int m, n;
183
   
182
   
184
        m = frame / 8;
183
        m = frame / 8;
185
        n = frame % 8;
184
        n = frame % 8;
186
   
185
   
187
        if ((*frame_bitmap_ptr)[m] & (1<<n)) {
186
        if ((*frame_bitmap_ptr)[m] & (1<<n)) {
188
            (*frame_bitmap_ptr)[m] &= ~(1<<n);
187
            (*frame_bitmap_ptr)[m] &= ~(1<<n);
189
            *frames_free_ptr++;
188
            *frames_free_ptr++;
190
            if (IS_KA(addr)) {
189
            if (IS_KA(addr)) {
191
                /*
190
                /*
192
                 * frames_free_ptr points to kernel_frames_free
191
                 * frames_free_ptr points to kernel_frames_free
193
                 * It is still necessary to increment frames_free.
192
                 * It is still necessary to increment frames_free.
194
                 */
193
                 */
195
                 frames_free++;
194
                 frames_free++;
196
            }  
195
            }  
197
        }
196
        }
198
        else panic("frame_free: frame already free\n");
197
        else panic("frame already free\n");
199
    }
198
    }
200
    else panic("frame_free: frame number too big\n");
199
    else panic("frame number too big\n");
201
   
200
   
202
    spinlock_unlock(&framelock);
201
    spinlock_unlock(&framelock);
203
    cpu_priority_restore(pri);
202
    cpu_priority_restore(pri);
204
}
203
}
205
 
204
 
206
/*
205
/*
207
 * Don't use this function for normal allocation. Use frame_alloc() instead.
206
 * Don't use this function for normal allocation. Use frame_alloc() instead.
208
 * Use this function to declare that some special frame is not free.
207
 * Use this function to declare that some special frame is not free.
209
 */
208
 */
210
void frame_not_free(__address addr)
209
void frame_not_free(__address addr)
211
{
210
{
212
    pri_t pri;
211
    pri_t pri;
213
    __u32 frame;
212
    __u32 frame;
214
    __u32 *frames_ptr = &frames, *frames_free_ptr = &frames_free;
213
    count_t *frames_ptr = &frames, *frames_free_ptr = &frames_free;
215
    __u8 **frame_bitmap_ptr = &frame_bitmap;
214
    __u8 **frame_bitmap_ptr = &frame_bitmap;
216
   
215
   
217
    pri = cpu_priority_high();
216
    pri = cpu_priority_high();
218
    spinlock_lock(&framelock);
217
    spinlock_lock(&framelock);
219
    frame = IS_KA(addr) ? KA2PA(addr) : addr;
218
    frame = IS_KA(addr) ? KA2PA(addr) : addr;
220
    frame /= FRAME_SIZE;
219
    frame /= FRAME_SIZE;
221
    if (frame < *frames_ptr) {
220
    if (frame < *frames_ptr) {
222
        int m, n;
221
        int m, n;
223
 
222
 
224
        m = frame / 8;
223
        m = frame / 8;
225
        n = frame % 8;
224
        n = frame % 8;
226
   
225
   
227
        if (((*frame_bitmap_ptr)[m] & (1<<n)) == 0) {  
226
        if (((*frame_bitmap_ptr)[m] & (1<<n)) == 0) {  
228
            (*frame_bitmap_ptr)[m] |= (1<<n);
227
            (*frame_bitmap_ptr)[m] |= (1<<n);
229
            *frames_free_ptr--;
228
            *frames_free_ptr--;
230
            if (IS_KA(addr)) {
229
            if (IS_KA(addr)) {
231
                /*
230
                /*
232
                 * frames_free_ptr points to kernel_frames_free
231
                 * frames_free_ptr points to kernel_frames_free
233
                 * It is still necessary to decrement frames_free.
232
                 * It is still necessary to decrement frames_free.
234
                 */
233
                 */
235
                frames_free--;
234
                frames_free--;
236
            }
235
            }
237
        }
236
        }
238
    }
237
    }
239
    spinlock_unlock(&framelock);
238
    spinlock_unlock(&framelock);
240
    cpu_priority_restore(pri);
239
    cpu_priority_restore(pri);
241
}
240
}
242
 
241
 
243
void frame_region_not_free(__address start, __address stop)
242
void frame_region_not_free(__address start, __address stop)
244
{
243
{
245
    __u32 i;
244
    __u32 i;
246
 
245
 
247
    start /= FRAME_SIZE;
246
    start /= FRAME_SIZE;
248
    stop /= FRAME_SIZE;
247
    stop /= FRAME_SIZE;
249
    for (i = start; i <= stop; i++)
248
    for (i = start; i <= stop; i++)
250
        frame_not_free(i * FRAME_SIZE);
249
        frame_not_free(i * FRAME_SIZE);
251
}
250
}
252
 
251