[f761f1eb] | 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>
|
---|
[169587a] | 32 | #include <mm/tlb.h>
|
---|
[30ef8ce] | 33 | #include <mm/heap.h>
|
---|
[f761f1eb] | 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>
|
---|
[18e0a6c] | 41 | #include <arch/asm.h>
|
---|
[b07769b6] | 42 | #include <debug.h>
|
---|
[9c0a9b3] | 43 | #include <memstr.h>
|
---|
| 44 | #include <arch.h>
|
---|
[f761f1eb] | 45 |
|
---|
[b07769b6] | 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)
|
---|
[f761f1eb] | 51 | {
|
---|
| 52 | vm_t *m;
|
---|
[f4a61ef] | 53 |
|
---|
[f761f1eb] | 54 | m = (vm_t *) malloc(sizeof(vm_t));
|
---|
| 55 | if (m) {
|
---|
| 56 | spinlock_initialize(&m->lock);
|
---|
| 57 | list_initialize(&m->vm_area_head);
|
---|
[b07769b6] | 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 | */
|
---|
[f4a61ef] | 63 | m->ptl0 = ptl0;
|
---|
| 64 | if (!m->ptl0) {
|
---|
[b07769b6] | 65 | pte_t *src_ptl0, *dst_ptl0;
|
---|
| 66 |
|
---|
[f4a61ef] | 67 | src_ptl0 = (pte_t *) PA2KA((__address) GET_PTL0_ADDRESS());
|
---|
[b07769b6] | 68 | dst_ptl0 = (pte_t *) frame_alloc(FRAME_KA | FRAME_PANIC);
|
---|
[623ba26c] | 69 |
|
---|
[399ccd9] | 70 | // memsetb((__address) dst_ptl0, PAGE_SIZE, 0);
|
---|
| 71 | // memcpy((void *) &dst_ptl0[KAS_START_INDEX], (void *) &src_ptl0[KAS_START_INDEX], KAS_INDICES);
|
---|
| 72 |
|
---|
[e515167d] | 73 | memcpy((void *) dst_ptl0,(void *) src_ptl0, PAGE_SIZE);
|
---|
[623ba26c] | 74 |
|
---|
[f4a61ef] | 75 | m->ptl0 = (pte_t *) KA2PA((__address) dst_ptl0);
|
---|
[b07769b6] | 76 | }
|
---|
[f761f1eb] | 77 | }
|
---|
[f4a61ef] | 78 |
|
---|
[f761f1eb] | 79 | return m;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void vm_destroy(vm_t *m)
|
---|
| 83 | {
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[6ba143d] | 86 | vm_area_t *vm_area_create(vm_t *m, vm_type_t type, size_t size, __address addr)
|
---|
[f761f1eb] | 87 | {
|
---|
| 88 | pri_t pri;
|
---|
| 89 | vm_area_t *a;
|
---|
| 90 |
|
---|
| 91 | if (addr % PAGE_SIZE)
|
---|
[02a99d2] | 92 | panic("addr not aligned to a page boundary");
|
---|
[f761f1eb] | 93 |
|
---|
| 94 | pri = cpu_priority_high();
|
---|
| 95 | spinlock_lock(&m->lock);
|
---|
| 96 |
|
---|
| 97 | /*
|
---|
| 98 | * TODO: test vm_area which is to be created doesn't overlap with an existing one.
|
---|
| 99 | */
|
---|
| 100 |
|
---|
| 101 | a = (vm_area_t *) malloc(sizeof(vm_area_t));
|
---|
| 102 | if (a) {
|
---|
| 103 | int i;
|
---|
| 104 |
|
---|
| 105 | a->mapping = (__address *) malloc(size * sizeof(__address));
|
---|
| 106 | if (!a->mapping) {
|
---|
| 107 | free(a);
|
---|
| 108 | spinlock_unlock(&m->lock);
|
---|
| 109 | cpu_priority_restore(pri);
|
---|
| 110 | return NULL;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | for (i=0; i<size; i++)
|
---|
| 114 | a->mapping[i] = frame_alloc(0);
|
---|
| 115 |
|
---|
| 116 | spinlock_initialize(&a->lock);
|
---|
| 117 |
|
---|
| 118 | link_initialize(&a->link);
|
---|
| 119 | a->type = type;
|
---|
| 120 | a->size = size;
|
---|
| 121 | a->address = addr;
|
---|
| 122 |
|
---|
| 123 | list_append(&a->link, &m->vm_area_head);
|
---|
| 124 |
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | spinlock_unlock(&m->lock);
|
---|
| 128 | cpu_priority_restore(pri);
|
---|
| 129 |
|
---|
| 130 | return a;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | void vm_area_destroy(vm_area_t *a)
|
---|
| 134 | {
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[b07769b6] | 137 | void vm_area_map(vm_area_t *a, vm_t *m)
|
---|
[f761f1eb] | 138 | {
|
---|
| 139 | int i, flags;
|
---|
| 140 | pri_t pri;
|
---|
| 141 |
|
---|
| 142 | pri = cpu_priority_high();
|
---|
[b07769b6] | 143 | spinlock_lock(&m->lock);
|
---|
[f761f1eb] | 144 | spinlock_lock(&a->lock);
|
---|
| 145 |
|
---|
| 146 | switch (a->type) {
|
---|
| 147 | case VMA_TEXT:
|
---|
| 148 | flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
|
---|
| 149 | break;
|
---|
| 150 | case VMA_DATA:
|
---|
| 151 | case VMA_STACK:
|
---|
| 152 | flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
|
---|
| 153 | break;
|
---|
| 154 | default:
|
---|
[b07769b6] | 155 | panic("unexpected vm_type_t %d", a->type);
|
---|
[f761f1eb] | 156 | }
|
---|
[b07769b6] | 157 |
|
---|
| 158 | ASSERT(m->ptl0);
|
---|
[f761f1eb] | 159 | for (i=0; i<a->size; i++)
|
---|
[b07769b6] | 160 | map_page_to_frame(a->address + i*PAGE_SIZE, a->mapping[i], flags, (__address) m->ptl0);
|
---|
[f761f1eb] | 161 |
|
---|
| 162 | spinlock_unlock(&a->lock);
|
---|
[b07769b6] | 163 | spinlock_unlock(&m->lock);
|
---|
[f761f1eb] | 164 | cpu_priority_restore(pri);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[b07769b6] | 167 | void vm_area_unmap(vm_area_t *a, vm_t *m)
|
---|
[f761f1eb] | 168 | {
|
---|
| 169 | int i;
|
---|
| 170 | pri_t pri;
|
---|
| 171 |
|
---|
| 172 | pri = cpu_priority_high();
|
---|
[b07769b6] | 173 | spinlock_lock(&m->lock);
|
---|
[f761f1eb] | 174 | spinlock_lock(&a->lock);
|
---|
| 175 |
|
---|
[b07769b6] | 176 | ASSERT(m->ptl0);
|
---|
[f761f1eb] | 177 | for (i=0; i<a->size; i++)
|
---|
[b07769b6] | 178 | map_page_to_frame(a->address + i*PAGE_SIZE, 0, PAGE_NOT_PRESENT, (__address) m->ptl0);
|
---|
[169587a] | 179 |
|
---|
[f761f1eb] | 180 | spinlock_unlock(&a->lock);
|
---|
| 181 | spinlock_unlock(&m->lock);
|
---|
| 182 | cpu_priority_restore(pri);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[b07769b6] | 185 | void vm_install(vm_t *m)
|
---|
[f761f1eb] | 186 | {
|
---|
| 187 | link_t *l;
|
---|
| 188 | pri_t pri;
|
---|
| 189 |
|
---|
| 190 | pri = cpu_priority_high();
|
---|
[169587a] | 191 |
|
---|
[b109ebb] | 192 | tlb_shootdown_start();
|
---|
[f761f1eb] | 193 | spinlock_lock(&m->lock);
|
---|
| 194 |
|
---|
[b07769b6] | 195 | ASSERT(m->ptl0);
|
---|
| 196 | SET_PTL0_ADDRESS(m->ptl0);
|
---|
[f761f1eb] | 197 |
|
---|
| 198 | spinlock_unlock(&m->lock);
|
---|
[b109ebb] | 199 | tlb_shootdown_finalize();
|
---|
[169587a] | 200 |
|
---|
[f761f1eb] | 201 | cpu_priority_restore(pri);
|
---|
| 202 | }
|
---|