Subversion Repositories HelenOS

Rev

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

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