Subversion Repositories HelenOS-historic

Rev

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

Rev 168 Rev 195
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 <mm/vm.h>
29
#include <mm/vm.h>
30
#include <mm/page.h>
30
#include <mm/page.h>
31
#include <mm/frame.h>
31
#include <mm/frame.h>
32
#include <mm/tlb.h>
32
#include <mm/tlb.h>
33
#include <mm/heap.h>
33
#include <mm/heap.h>
34
#include <arch/mm/page.h>
34
#include <arch/mm/page.h>
35
#include <arch/types.h>
35
#include <arch/types.h>
36
#include <typedefs.h>
36
#include <typedefs.h>
37
#include <synch/spinlock.h>
37
#include <synch/spinlock.h>
38
#include <config.h>
38
#include <config.h>
39
#include <list.h>
39
#include <list.h>
40
#include <panic.h>
40
#include <panic.h>
41
#include <arch/asm.h>
41
#include <arch/asm.h>
42
#include <debug.h>
42
#include <debug.h>
-
 
43
#include <memstr.h>
-
 
44
#include <arch.h>
43
 
45
 
44
#define KAS_START_INDEX     PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)
46
#define KAS_START_INDEX     PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)
45
#define KAS_END_INDEX       PTL0_INDEX(KERNEL_ADDRESS_SPACE_END)
47
#define KAS_END_INDEX       PTL0_INDEX(KERNEL_ADDRESS_SPACE_END)
46
#define KAS_INDICES     (1+(KAS_END_INDEX-KAS_START_INDEX))
48
#define KAS_INDICES     (1+(KAS_END_INDEX-KAS_START_INDEX))
47
 
49
 
48
vm_t *vm_create(pte_t *ptl0)
50
vm_t *vm_create(pte_t *ptl0)
49
{
51
{
50
    vm_t *m;
52
    vm_t *m;
51
 
53
 
52
    m = (vm_t *) malloc(sizeof(vm_t));
54
    m = (vm_t *) malloc(sizeof(vm_t));
53
    if (m) {
55
    if (m) {
54
        spinlock_initialize(&m->lock);
56
        spinlock_initialize(&m->lock);
55
        list_initialize(&m->vm_area_head);
57
        list_initialize(&m->vm_area_head);
56
 
58
 
57
        /*
59
        /*
58
         * Each vm_t is supposed to have its own page table.
60
         * Each vm_t is supposed to have its own page table.
59
         * It is either passed one or it has to allocate and set one up.
61
         * It is either passed one or it has to allocate and set one up.
60
         */
62
         */
61
        m->ptl0 = ptl0;
63
        m->ptl0 = ptl0;
62
        if (!m->ptl0) {
64
        if (!m->ptl0) {
63
            pte_t *src_ptl0, *dst_ptl0;
65
            pte_t *src_ptl0, *dst_ptl0;
64
       
66
       
65
            src_ptl0 = (pte_t *) PA2KA((__address) GET_PTL0_ADDRESS());
67
            src_ptl0 = (pte_t *) PA2KA((__address) GET_PTL0_ADDRESS());
66
            dst_ptl0 = (pte_t *) frame_alloc(FRAME_KA | FRAME_PANIC);
68
            dst_ptl0 = (pte_t *) frame_alloc(FRAME_KA | FRAME_PANIC);
67
//          memsetb((__address) dst_ptl0, PAGE_SIZE, 0);
69
//          memsetb((__address) dst_ptl0, PAGE_SIZE, 0);
68
//          memcopy((__address) &src_ptl0[KAS_START_INDEX], (__address) &dst_ptl0[KAS_START_INDEX], KAS_INDICES*sizeof(pte_t));
70
//          memcopy((__address) &src_ptl0[KAS_START_INDEX], (__address) &dst_ptl0[KAS_START_INDEX], KAS_INDICES*sizeof(pte_t));
69
            memcopy(PA2KA((__address) GET_PTL0_ADDRESS()), (__address) dst_ptl0, PAGE_SIZE);
71
            memcopy((void *)PA2KA((__address) dst_ptl0), (void *)GET_PTL0_ADDRESS() , PAGE_SIZE);
70
            m->ptl0 = (pte_t *) KA2PA((__address) dst_ptl0);
72
            m->ptl0 = (pte_t *) KA2PA((__address) dst_ptl0);
71
        }
73
        }
72
    }
74
    }
73
 
75
 
74
    return m;
76
    return m;
75
}
77
}
76
 
78
 
77
void vm_destroy(vm_t *m)
79
void vm_destroy(vm_t *m)
78
{
80
{
79
}
81
}
80
 
82
 
81
vm_area_t *vm_area_create(vm_t *m, vm_type_t type, size_t size, __address addr)
83
vm_area_t *vm_area_create(vm_t *m, vm_type_t type, size_t size, __address addr)
82
{
84
{
83
    pri_t pri;
85
    pri_t pri;
84
    vm_area_t *a;
86
    vm_area_t *a;
85
   
87
   
86
    if (addr % PAGE_SIZE)
88
    if (addr % PAGE_SIZE)
87
        panic("addr not aligned to a page boundary");
89
        panic("addr not aligned to a page boundary");
88
   
90
   
89
    pri = cpu_priority_high();
91
    pri = cpu_priority_high();
90
    spinlock_lock(&m->lock);
92
    spinlock_lock(&m->lock);
91
   
93
   
92
    /*
94
    /*
93
     * TODO: test vm_area which is to be created doesn't overlap with an existing one.
95
     * TODO: test vm_area which is to be created doesn't overlap with an existing one.
94
     */
96
     */
95
   
97
   
96
    a = (vm_area_t *) malloc(sizeof(vm_area_t));
98
    a = (vm_area_t *) malloc(sizeof(vm_area_t));
97
    if (a) {
99
    if (a) {
98
        int i;
100
        int i;
99
   
101
   
100
        a->mapping = (__address *) malloc(size * sizeof(__address));
102
        a->mapping = (__address *) malloc(size * sizeof(__address));
101
        if (!a->mapping) {
103
        if (!a->mapping) {
102
            free(a);
104
            free(a);
103
            spinlock_unlock(&m->lock);
105
            spinlock_unlock(&m->lock);
104
            cpu_priority_restore(pri);
106
            cpu_priority_restore(pri);
105
            return NULL;
107
            return NULL;
106
        }
108
        }
107
       
109
       
108
        for (i=0; i<size; i++)
110
        for (i=0; i<size; i++)
109
            a->mapping[i] = frame_alloc(0);
111
            a->mapping[i] = frame_alloc(0);
110
       
112
       
111
        spinlock_initialize(&a->lock);
113
        spinlock_initialize(&a->lock);
112
           
114
           
113
        link_initialize(&a->link);         
115
        link_initialize(&a->link);         
114
        a->type = type;
116
        a->type = type;
115
        a->size = size;
117
        a->size = size;
116
        a->address = addr;
118
        a->address = addr;
117
       
119
       
118
        list_append(&a->link, &m->vm_area_head);
120
        list_append(&a->link, &m->vm_area_head);
119
 
121
 
120
    }
122
    }
121
 
123
 
122
    spinlock_unlock(&m->lock);
124
    spinlock_unlock(&m->lock);
123
    cpu_priority_restore(pri);
125
    cpu_priority_restore(pri);
124
   
126
   
125
    return a;
127
    return a;
126
}
128
}
127
 
129
 
128
void vm_area_destroy(vm_area_t *a)
130
void vm_area_destroy(vm_area_t *a)
129
{
131
{
130
}
132
}
131
 
133
 
132
void vm_area_map(vm_area_t *a, vm_t *m)
134
void vm_area_map(vm_area_t *a, vm_t *m)
133
{
135
{
134
    int i, flags;
136
    int i, flags;
135
    pri_t pri;
137
    pri_t pri;
136
   
138
   
137
    pri = cpu_priority_high();
139
    pri = cpu_priority_high();
138
    spinlock_lock(&m->lock);
140
    spinlock_lock(&m->lock);
139
    spinlock_lock(&a->lock);
141
    spinlock_lock(&a->lock);
140
 
142
 
141
    switch (a->type) {
143
    switch (a->type) {
142
        case VMA_TEXT:
144
        case VMA_TEXT:
143
            flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
145
            flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
144
            break;
146
            break;
145
        case VMA_DATA:
147
        case VMA_DATA:
146
        case VMA_STACK:
148
        case VMA_STACK:
147
            flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
149
            flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
148
            break;
150
            break;
149
        default:
151
        default:
150
            panic("unexpected vm_type_t %d", a->type);
152
            panic("unexpected vm_type_t %d", a->type);
151
    }
153
    }
152
 
154
 
153
    ASSERT(m->ptl0);
155
    ASSERT(m->ptl0);
154
    for (i=0; i<a->size; i++)
156
    for (i=0; i<a->size; i++)
155
        map_page_to_frame(a->address + i*PAGE_SIZE, a->mapping[i], flags, (__address) m->ptl0);
157
        map_page_to_frame(a->address + i*PAGE_SIZE, a->mapping[i], flags, (__address) m->ptl0);
156
       
158
       
157
    spinlock_unlock(&a->lock);
159
    spinlock_unlock(&a->lock);
158
    spinlock_unlock(&m->lock);
160
    spinlock_unlock(&m->lock);
159
    cpu_priority_restore(pri);
161
    cpu_priority_restore(pri);
160
}
162
}
161
 
163
 
162
void vm_area_unmap(vm_area_t *a, vm_t *m)
164
void vm_area_unmap(vm_area_t *a, vm_t *m)
163
{
165
{
164
    int i;
166
    int i;
165
    pri_t pri;
167
    pri_t pri;
166
   
168
   
167
    pri = cpu_priority_high();
169
    pri = cpu_priority_high();
168
    spinlock_lock(&m->lock);
170
    spinlock_lock(&m->lock);
169
    spinlock_lock(&a->lock);
171
    spinlock_lock(&a->lock);
170
 
172
 
171
    ASSERT(m->ptl0);
173
    ASSERT(m->ptl0);
172
    for (i=0; i<a->size; i++)      
174
    for (i=0; i<a->size; i++)      
173
        map_page_to_frame(a->address + i*PAGE_SIZE, 0, PAGE_NOT_PRESENT, (__address) m->ptl0);
175
        map_page_to_frame(a->address + i*PAGE_SIZE, 0, PAGE_NOT_PRESENT, (__address) m->ptl0);
174
   
176
   
175
    spinlock_unlock(&a->lock);
177
    spinlock_unlock(&a->lock);
176
    spinlock_unlock(&m->lock);
178
    spinlock_unlock(&m->lock);
177
    cpu_priority_restore(pri);
179
    cpu_priority_restore(pri);
178
}
180
}
179
 
181
 
180
void vm_install(vm_t *m)
182
void vm_install(vm_t *m)
181
{
183
{
182
    link_t *l;
184
    link_t *l;
183
    pri_t pri;
185
    pri_t pri;
184
   
186
   
185
    pri = cpu_priority_high();
187
    pri = cpu_priority_high();
186
 
188
 
187
    tlb_shootdown_start();
189
    tlb_shootdown_start();
188
    spinlock_lock(&m->lock);
190
    spinlock_lock(&m->lock);
189
 
191
 
190
    ASSERT(m->ptl0);
192
    ASSERT(m->ptl0);
191
    SET_PTL0_ADDRESS(m->ptl0);
193
    SET_PTL0_ADDRESS(m->ptl0);
192
 
194
 
193
    spinlock_unlock(&m->lock);
195
    spinlock_unlock(&m->lock);
194
    tlb_shootdown_finalize();
196
    tlb_shootdown_finalize();
195
 
197
 
196
    cpu_priority_restore(pri);
198
    cpu_priority_restore(pri);
197
}
199
}
198
 
200