Subversion Repositories HelenOS-historic

Rev

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

Rev 113 Rev 115
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
#include <arch/asm.h>
-
 
46
 
45
count_t frames = 0;
47
count_t frames = 0;
46
count_t frames_free;
48
count_t frames_free;
47
 
49
 
48
__u8 *frame_bitmap;
50
__u8 *frame_bitmap;
49
count_t frame_bitmap_octets;
51
count_t frame_bitmap_octets;
50
 
52
 
51
static spinlock_t framelock;
53
static spinlock_t framelock;
52
 
54
 
53
void frame_init(void)
55
void frame_init(void)
54
{
56
{
55
        if (config.cpu_active == 1) {
57
        if (config.cpu_active == 1) {
56
 
58
 
57
                /*
59
                /*
58
                 * The bootstrap processor will allocate all necessary memory for frame allocation.
60
                 * The bootstrap processor will allocate all necessary memory for frame allocation.
59
                 */
61
                 */
60
 
62
 
61
                frames = config.memory_size / FRAME_SIZE;
63
                frames = config.memory_size / FRAME_SIZE;
62
                frame_bitmap_octets = frames / 8 + (frames % 8 > 0);
64
                frame_bitmap_octets = frames / 8 + (frames % 8 > 0);
63
                frame_bitmap = (__u8 *) malloc(frame_bitmap_octets);
65
                frame_bitmap = (__u8 *) malloc(frame_bitmap_octets);
64
                if (!frame_bitmap)
66
                if (!frame_bitmap)
65
                        panic("malloc/frame_bitmap\n");
67
                        panic("malloc/frame_bitmap\n");
66
 
68
 
67
                /*
69
                /*
68
                 * Mark all frames free.
70
                 * Mark all frames free.
69
                 */
71
                 */
70
                memsetb((__address) frame_bitmap, frame_bitmap_octets, 0);
72
                memsetb((__address) frame_bitmap, frame_bitmap_octets, 0);
71
                frames_free = frames;
73
                frames_free = frames;
72
    }
74
    }
73
 
75
 
74
    /*
76
    /*
75
     * No frame allocations/reservations prior this point.
77
     * No frame allocations/reservations prior this point.
76
     */
78
     */
77
 
79
 
78
    frame_arch_init();
80
    frame_arch_init();
79
 
81
 
80
    if (config.cpu_active == 1) {
82
    if (config.cpu_active == 1) {
81
                /*
83
                /*
82
                 * Create the memory address space map. Marked frames and frame
84
                 * Create the memory address space map. Marked frames and frame
83
                 * regions cannot be used for allocation.
85
                 * regions cannot be used for allocation.
84
                 */
86
                 */
85
        frame_region_not_free(config.base, config.base + config.kernel_size);
87
        frame_region_not_free(config.base, config.base + config.kernel_size);
86
    }
88
    }
87
}
89
}
88
 
90
 
89
/*
91
/*
90
 * Allocate a frame.
92
 * Allocate a frame.
91
 */
93
 */
92
__address frame_alloc(int flags)
94
__address frame_alloc(int flags)
93
{
95
{
94
    int i;
96
    int i;
95
    pri_t pri;
97
    pri_t pri;
96
   
98
   
97
loop:
99
loop:
98
    pri = cpu_priority_high();
100
    pri = cpu_priority_high();
99
    spinlock_lock(&framelock);
101
    spinlock_lock(&framelock);
100
    if (frames_free) {
102
    if (frames_free) {
101
        for (i=0; i < frames; i++) {
103
        for (i=0; i < frames; i++) {
102
            int m, n;
104
            int m, n;
103
       
105
       
104
            m = i / 8;
106
            m = i / 8;
105
            n = i % 8;
107
            n = i % 8;
106
 
108
 
107
            if ((frame_bitmap[m] & (1<<n)) == 0) {
109
            if ((frame_bitmap[m] & (1<<n)) == 0) {
108
                frame_bitmap[m] |= (1<<n);
110
                frame_bitmap[m] |= (1<<n);
109
                frames_free--;
111
                frames_free--;
110
                spinlock_unlock(&framelock);
112
                spinlock_unlock(&framelock);
111
                cpu_priority_restore(pri);
113
                cpu_priority_restore(pri);
112
                if (flags & FRAME_KA) return PA2KA(i*FRAME_SIZE);
114
                if (flags & FRAME_KA) return PA2KA(i*FRAME_SIZE);
113
                return i*FRAME_SIZE;
115
                return i*FRAME_SIZE;
114
            }
116
            }
115
        }
117
        }
116
        panic("frames_free inconsistent (%d)\n", frames_free);
118
        panic("frames_free inconsistent (%d)\n", frames_free);
117
    }
119
    }
118
    spinlock_unlock(&framelock);
120
    spinlock_unlock(&framelock);
119
    cpu_priority_restore(pri);
121
    cpu_priority_restore(pri);
120
 
122
 
121
    if (flags & FRAME_PANIC)
123
    if (flags & FRAME_PANIC)
122
        panic("unable to allocate frame\n");
124
        panic("unable to allocate frame\n");
123
       
125
       
124
    /* TODO: implement sleeping logic here */
126
    /* TODO: implement sleeping logic here */
125
    panic("sleep not supported\n");
127
    panic("sleep not supported\n");
126
   
128
   
127
    goto loop;
129
    goto loop;
128
}
130
}
129
 
131
 
130
/*
132
/*
131
 * Free a frame.
133
 * Free a frame.
132
 */
134
 */
133
void frame_free(__address addr)
135
void frame_free(__address addr)
134
{
136
{
135
    pri_t pri;
137
    pri_t pri;
136
    __u32 frame;
138
    __u32 frame;
137
 
139
 
138
    pri = cpu_priority_high();
140
    pri = cpu_priority_high();
139
    spinlock_lock(&framelock);
141
    spinlock_lock(&framelock);
140
   
142
   
141
    frame = IS_KA(addr) ? KA2PA(addr) : addr;
143
    frame = IS_KA(addr) ? KA2PA(addr) : addr;
142
    frame /= FRAME_SIZE;
144
    frame /= FRAME_SIZE;
143
    if (frame < frames) {
145
    if (frame < frames) {
144
        int m, n;
146
        int m, n;
145
   
147
   
146
        m = frame / 8;
148
        m = frame / 8;
147
        n = frame % 8;
149
        n = frame % 8;
148
   
150
   
149
        if (frame_bitmap[m] & (1<<n)) {
151
        if (frame_bitmap[m] & (1<<n)) {
150
            frame_bitmap[m] &= ~(1<<n);
152
            frame_bitmap[m] &= ~(1<<n);
151
            frames_free++;
153
            frames_free++;
152
        }
154
        }
153
        else panic("frame already free\n");
155
        else panic("frame already free\n");
154
    }
156
    }
155
    else panic("frame number too big\n");
157
    else panic("frame number too big\n");
156
   
158
   
157
    spinlock_unlock(&framelock);
159
    spinlock_unlock(&framelock);
158
    cpu_priority_restore(pri);
160
    cpu_priority_restore(pri);
159
}
161
}
160
 
162
 
161
/*
163
/*
162
 * Don't use this function for normal allocation. Use frame_alloc() instead.
164
 * Don't use this function for normal allocation. Use frame_alloc() instead.
163
 * Use this function to declare that some special frame is not free.
165
 * Use this function to declare that some special frame is not free.
164
 */
166
 */
165
void frame_not_free(__address addr)
167
void frame_not_free(__address addr)
166
{
168
{
167
    pri_t pri;
169
    pri_t pri;
168
    __u32 frame;
170
    __u32 frame;
169
   
171
   
170
    pri = cpu_priority_high();
172
    pri = cpu_priority_high();
171
    spinlock_lock(&framelock);
173
    spinlock_lock(&framelock);
172
    frame = IS_KA(addr) ? KA2PA(addr) : addr;
174
    frame = IS_KA(addr) ? KA2PA(addr) : addr;
173
    frame /= FRAME_SIZE;
175
    frame /= FRAME_SIZE;
174
    if (frame < frames) {
176
    if (frame < frames) {
175
        int m, n;
177
        int m, n;
176
 
178
 
177
        m = frame / 8;
179
        m = frame / 8;
178
        n = frame % 8;
180
        n = frame % 8;
179
   
181
   
180
        if ((frame_bitmap[m] & (1<<n)) == 0) { 
182
        if ((frame_bitmap[m] & (1<<n)) == 0) { 
181
            frame_bitmap[m] |= (1<<n);
183
            frame_bitmap[m] |= (1<<n);
182
            frames_free--; 
184
            frames_free--; 
183
        }
185
        }
184
    }
186
    }
185
    spinlock_unlock(&framelock);
187
    spinlock_unlock(&framelock);
186
    cpu_priority_restore(pri);
188
    cpu_priority_restore(pri);
187
}
189
}
188
 
190
 
189
void frame_region_not_free(__address start, __address stop)
191
void frame_region_not_free(__address start, __address stop)
190
{
192
{
191
    __u32 i;
193
    __u32 i;
192
 
194
 
193
    start /= FRAME_SIZE;
195
    start /= FRAME_SIZE;
194
    stop /= FRAME_SIZE;
196
    stop /= FRAME_SIZE;
195
    for (i = start; i <= stop; i++)
197
    for (i = start; i <= stop; i++)
196
        frame_not_free(i * FRAME_SIZE);
198
        frame_not_free(i * FRAME_SIZE);
197
}
199
}
198
 
200