Subversion Repositories HelenOS

Rev

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