Subversion Repositories HelenOS-historic

Rev

Rev 68 | Rev 113 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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