summaryrefslogtreecommitdiff
path: root/vm/vm_kmem.c
blob: 7de04bd8692e8e7eeb6dddb81eef11be018e35c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright (c) 2011-2017 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * TODO Rework so that pmap update errors can be handled.
 */

#include <kern/assert.h>
#include <kern/cpumap.h>
#include <kern/init.h>
#include <kern/panic.h>
#include <kern/param.h>
#include <kern/stddef.h>
#include <kern/stdint.h>
#include <kern/types.h>
#include <machine/pmap.h>
#include <vm/vm_adv.h>
#include <vm/vm_inherit.h>
#include <vm/vm_kmem.h>
#include <vm/vm_map.h>
#include <vm/vm_page.h>
#include <vm/vm_prot.h>

/*
 * Kernel map and storage.
 */
static struct vm_map kernel_map_store;
struct vm_map *kernel_map __read_mostly = &kernel_map_store;

static int
vm_kmem_alloc_check(size_t size)
{
    if (!vm_page_aligned(size)
        || (size == 0)) {
        return -1;
    }

    return 0;
}

static int
vm_kmem_free_check(uintptr_t va, size_t size)
{
    if (!vm_page_aligned(va)) {
        return -1;
    }

    return vm_kmem_alloc_check(size);
}

void *
vm_kmem_alloc_va(size_t size)
{
    int error, flags;
    uintptr_t va;

    assert(vm_kmem_alloc_check(size) == 0);

    va = 0;
    flags = VM_MAP_FLAGS(VM_PROT_ALL, VM_PROT_ALL, VM_INHERIT_NONE,
                         VM_ADV_DEFAULT, 0);
    error = vm_map_enter(kernel_map, &va, size, 0, flags, NULL, 0);

    if (error) {
        return 0;
    }

    return (void *)va;
}

void
vm_kmem_free_va(void *addr, size_t size)
{
    uintptr_t va;

    va = (uintptr_t)addr;
    assert(vm_kmem_free_check(va, size) == 0);
    vm_map_remove(kernel_map, va, va + vm_page_round(size));
}

void *
vm_kmem_alloc(size_t size)
{
    struct vm_page *page;
    uintptr_t va, start, end;

    size = vm_page_round(size);
    va = (uintptr_t)vm_kmem_alloc_va(size);

    if (va == 0) {
        return 0;
    }

    for (start = va, end = va + size; start < end; start += PAGE_SIZE) {
        page = vm_page_alloc(0, VM_PAGE_SEL_HIGHMEM, VM_PAGE_KERNEL);

        if (page == NULL) {
            goto error_page;
        }

        pmap_enter(kernel_pmap, start, vm_page_to_pa(page),
                   VM_PROT_READ | VM_PROT_WRITE, PMAP_PEF_GLOBAL);
    }

    pmap_update(kernel_pmap);
    return (void *)va;

error_page:
    size = start - va;

    if (size != 0) {
        pmap_update(kernel_pmap);
        vm_kmem_free((void *)va, size);
    }

    size = end - start;

    if (size != 0) {
        vm_kmem_free_va((void *)start, size);
    }

    return NULL;
}

void
vm_kmem_free(void *addr, size_t size)
{
    const struct cpumap *cpumap;
    struct vm_page *page;
    uintptr_t va, end;
    phys_addr_t pa;
    int error;

    va = (uintptr_t)addr;
    size = vm_page_round(size);
    end = va + size;
    cpumap = cpumap_all();

    while (va < end) {
        error = pmap_kextract(va, &pa);
        assert(!error);
        pmap_remove(kernel_pmap, va, cpumap);
        page = vm_page_lookup(pa);
        assert(page != NULL);
        vm_page_free(page, 0);
        va += PAGE_SIZE;
    }

    pmap_update(kernel_pmap);
    vm_kmem_free_va(addr, size);
}

void *
vm_kmem_map_pa(phys_addr_t pa, size_t size,
               uintptr_t *map_vap, size_t *map_sizep)
{
    uintptr_t offset, map_va;
    size_t map_size;
    phys_addr_t start;

    start = vm_page_trunc(pa);
    map_size = vm_page_round(pa + size) - start;
    map_va = (uintptr_t)vm_kmem_alloc_va(map_size);

    if (map_va == 0) {
        return NULL;
    }

    for (offset = 0; offset < map_size; offset += PAGE_SIZE)
        pmap_enter(kernel_pmap, map_va + offset, start + offset,
                   VM_PROT_READ | VM_PROT_WRITE, PMAP_PEF_GLOBAL);

    pmap_update(kernel_pmap);

    if (map_vap != NULL) {
        *map_vap = map_va;
    }

    if (map_sizep != NULL) {
        *map_sizep = map_size;
    }

    return (void *)(map_va + (uintptr_t)(pa & PAGE_MASK));
}

void
vm_kmem_unmap_pa(uintptr_t map_va, size_t map_size)
{
    const struct cpumap *cpumap;
    uintptr_t va, end;

    cpumap = cpumap_all();
    end = map_va + map_size;

    for (va = map_va; va < end; va += PAGE_SIZE) {
        pmap_remove(kernel_pmap, va, cpumap);
    }

    pmap_update(kernel_pmap);
    vm_kmem_free_va((void *)map_va, map_size);
}