Subversion Repositories HelenOS-historic

Rev

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

Rev 1 Rev 5
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 <arch/mm/page.h>
33
#include <arch/mm/page.h>
33
#include <arch/types.h>
34
#include <arch/types.h>
34
#include <typedefs.h>
35
#include <typedefs.h>
35
#include <synch/spinlock.h>
36
#include <synch/spinlock.h>
36
#include <config.h>
37
#include <config.h>
37
#include <list.h>
38
#include <list.h>
38
#include <panic.h>
39
#include <panic.h>
39
 
40
 
40
vm_t *vm_create(void)
41
vm_t *vm_create(void)
41
{
42
{
42
    vm_t *m;
43
    vm_t *m;
43
   
44
   
44
    m = (vm_t *) malloc(sizeof(vm_t));
45
    m = (vm_t *) malloc(sizeof(vm_t));
45
    if (m) {
46
    if (m) {
46
        spinlock_initialize(&m->lock);
47
        spinlock_initialize(&m->lock);
47
        list_initialize(&m->vm_area_head);
48
        list_initialize(&m->vm_area_head);
48
    }
49
    }
49
   
50
   
50
    return m;
51
    return m;
51
}
52
}
52
 
53
 
53
void vm_destroy(vm_t *m)
54
void vm_destroy(vm_t *m)
54
{
55
{
55
}
56
}
56
 
57
 
57
vm_area_t *vm_area_create(vm_t *m, vm_type_t type, int size, __address addr)
58
vm_area_t *vm_area_create(vm_t *m, vm_type_t type, int size, __address addr)
58
{
59
{
59
    pri_t pri;
60
    pri_t pri;
60
    vm_area_t *a;
61
    vm_area_t *a;
61
   
62
   
62
    if (addr % PAGE_SIZE)
63
    if (addr % PAGE_SIZE)
63
        panic(PANIC "addr not aligned to a page boundary");
64
        panic(PANIC "addr not aligned to a page boundary");
64
   
65
   
65
    pri = cpu_priority_high();
66
    pri = cpu_priority_high();
66
    spinlock_lock(&m->lock);
67
    spinlock_lock(&m->lock);
67
   
68
   
68
    /*
69
    /*
69
     * TODO: test vm_area which is to be created doesn't overlap with an existing one.
70
     * TODO: test vm_area which is to be created doesn't overlap with an existing one.
70
     */
71
     */
71
   
72
   
72
    a = (vm_area_t *) malloc(sizeof(vm_area_t));
73
    a = (vm_area_t *) malloc(sizeof(vm_area_t));
73
    if (a) {
74
    if (a) {
74
        int i;
75
        int i;
75
   
76
   
76
        a->mapping = (__address *) malloc(size * sizeof(__address));
77
        a->mapping = (__address *) malloc(size * sizeof(__address));
77
        if (!a->mapping) {
78
        if (!a->mapping) {
78
            free(a);
79
            free(a);
79
            spinlock_unlock(&m->lock);
80
            spinlock_unlock(&m->lock);
80
            cpu_priority_restore(pri);
81
            cpu_priority_restore(pri);
81
            return NULL;
82
            return NULL;
82
        }
83
        }
83
       
84
       
84
        for (i=0; i<size; i++)
85
        for (i=0; i<size; i++)
85
            a->mapping[i] = frame_alloc(0);
86
            a->mapping[i] = frame_alloc(0);
86
       
87
       
87
        spinlock_initialize(&a->lock);
88
        spinlock_initialize(&a->lock);
88
           
89
           
89
        link_initialize(&a->link);         
90
        link_initialize(&a->link);         
90
        a->type = type;
91
        a->type = type;
91
        a->size = size;
92
        a->size = size;
92
        a->address = addr;
93
        a->address = addr;
93
       
94
       
94
        list_append(&a->link, &m->vm_area_head);
95
        list_append(&a->link, &m->vm_area_head);
95
 
96
 
96
    }
97
    }
97
 
98
 
98
    spinlock_unlock(&m->lock);
99
    spinlock_unlock(&m->lock);
99
    cpu_priority_restore(pri);
100
    cpu_priority_restore(pri);
100
   
101
   
101
    return a;
102
    return a;
102
}
103
}
103
 
104
 
104
void vm_area_destroy(vm_area_t *a)
105
void vm_area_destroy(vm_area_t *a)
105
{
106
{
106
}
107
}
107
 
108
 
108
void vm_area_map(vm_area_t *a)
109
void vm_area_map(vm_area_t *a)
109
{
110
{
110
    int i, flags;
111
    int i, flags;
111
    pri_t pri;
112
    pri_t pri;
112
   
113
   
113
    pri = cpu_priority_high();
114
    pri = cpu_priority_high();
114
    spinlock_lock(&a->lock);
115
    spinlock_lock(&a->lock);
115
 
116
 
116
    switch (a->type) {
117
    switch (a->type) {
117
        case VMA_TEXT:
118
        case VMA_TEXT:
118
            flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
119
            flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
119
            break;
120
            break;
120
        case VMA_DATA:
121
        case VMA_DATA:
121
        case VMA_STACK:
122
        case VMA_STACK:
122
            flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
123
            flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
123
            break;
124
            break;
124
        default:
125
        default:
125
            panic(PANIC "unexpected vm_type_t %d", a->type);
126
            panic(PANIC "unexpected vm_type_t %d", a->type);
126
    }
127
    }
127
   
128
   
128
    for (i=0; i<a->size; i++)
129
    for (i=0; i<a->size; i++)
129
        map_page_to_frame(a->address + i*PAGE_SIZE, a->mapping[i], flags, 0);
130
        map_page_to_frame(a->address + i*PAGE_SIZE, a->mapping[i], flags, 0);
130
       
131
       
131
    spinlock_unlock(&a->lock);
132
    spinlock_unlock(&a->lock);
132
    cpu_priority_restore(pri);
133
    cpu_priority_restore(pri);
133
}
134
}
134
 
135
 
135
void vm_area_unmap(vm_area_t *a)
136
void vm_area_unmap(vm_area_t *a)
136
{
137
{
137
    int i;
138
    int i;
138
    pri_t pri;
139
    pri_t pri;
139
   
140
   
140
    pri = cpu_priority_high();
141
    pri = cpu_priority_high();
141
    spinlock_lock(&a->lock);
142
    spinlock_lock(&a->lock);
142
 
143
 
143
    for (i=0; i<a->size; i++)      
144
    for (i=0; i<a->size; i++)      
144
        map_page_to_frame(a->address + i*PAGE_SIZE, 0, PAGE_NOT_PRESENT, 0);
145
        map_page_to_frame(a->address + i*PAGE_SIZE, 0, PAGE_NOT_PRESENT, 0);
145
       
146
   
146
    spinlock_unlock(&a->lock);
147
    spinlock_unlock(&a->lock);
147
    cpu_priority_restore(pri);
148
    cpu_priority_restore(pri);
148
}
149
}
149
 
150
 
150
void vm_install(vm_t *m)
151
void vm_install(vm_t *m)
151
{
152
{
152
    link_t *l;
153
    link_t *l;
153
    pri_t pri;
154
    pri_t pri;
154
   
155
   
155
    pri = cpu_priority_high();
156
    pri = cpu_priority_high();
156
    spinlock_lock(&m->lock);
157
    spinlock_lock(&m->lock);
157
 
158
 
158
    for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next)
159
    for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next)
159
        vm_area_map(list_get_instance(l, vm_area_t, link));
160
        vm_area_map(list_get_instance(l, vm_area_t, link));
160
 
161
 
161
    spinlock_unlock(&m->lock);
162
    spinlock_unlock(&m->lock);
162
    cpu_priority_restore(pri);
163
    cpu_priority_restore(pri);
163
}
164
}
164
 
165
 
165
void vm_uninstall(vm_t *m)
166
void vm_uninstall(vm_t *m)
166
{
167
{
167
    link_t *l;
168
    link_t *l;
168
    pri_t pri;
169
    pri_t pri;
169
   
170
   
170
    pri = cpu_priority_high();
171
    pri = cpu_priority_high();
-
 
172
 
-
 
173
    tlb_shutdown_start();
-
 
174
 
171
    spinlock_lock(&m->lock);
175
    spinlock_lock(&m->lock);
172
 
176
 
173
    for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next)
177
    for(l = m->vm_area_head.next; l != &m->vm_area_head; l = l->next)
174
        vm_area_unmap(list_get_instance(l, vm_area_t, link));
178
        vm_area_unmap(list_get_instance(l, vm_area_t, link));
175
 
179
 
176
    spinlock_unlock(&m->lock);
180
    spinlock_unlock(&m->lock);
-
 
181
 
-
 
182
    tlb_invalidate(0);
-
 
183
    tlb_shutdown_finalize();
-
 
184
 
177
    cpu_priority_restore(pri);
185
    cpu_priority_restore(pri);
178
}
186
}
179
 
187