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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
#include <lib.h>
#include <comus/memory.h>
#include <stdint.h>
#include "virtalloc.h"
extern char kernel_start[];
extern char kernel_end[];
static struct virt_addr_node *get_node_idx(struct virt_ctx *ctx, int idx)
{
if (idx < BOOTSTRAP_VIRT_ALLOC_NODES) {
return &ctx->bootstrap_nodes[idx];
} else {
return &ctx->alloc_nodes[idx - BOOTSTRAP_VIRT_ALLOC_NODES];
}
}
static void update_node_ptrs(struct virt_addr_node *old,
struct virt_addr_node *new, int old_len,
int new_len)
{
if (old == NULL)
return;
int idx = 0;
for (int i = 0; i < old_len; i++) {
struct virt_addr_node *o = &old[i];
if (o && !o->is_used)
continue;
struct virt_addr_node *n = &new[idx++];
*n = *o;
if (n->prev != NULL)
n->prev->next = n;
if (n->next != NULL)
n->next->prev = n;
}
for (int i = idx; i < new_len; i++) {
struct virt_addr_node *n = &new[idx++];
n->is_used = false;
}
}
static struct virt_addr_node *get_node(struct virt_ctx *ctx)
{
size_t count = BOOTSTRAP_VIRT_ALLOC_NODES + ctx->alloc_node_count;
if (!ctx->is_allocating && ctx->used_node_count + 16 >= count) {
ctx->is_allocating = true;
int new_alloc = ctx->alloc_node_count * 2;
if (new_alloc < 8)
new_alloc = 8;
struct virt_addr_node *new_nodes;
new_nodes = kalloc(sizeof(struct virt_addr_node) * new_alloc);
if (new_nodes == NULL)
panic("virt addr alloc nodes is null");
update_node_ptrs(ctx->alloc_nodes, new_nodes, ctx->alloc_node_count,
new_alloc);
kfree(ctx->alloc_nodes);
ctx->alloc_nodes = new_nodes;
ctx->alloc_node_count = new_alloc;
ctx->is_allocating = false;
count = BOOTSTRAP_VIRT_ALLOC_NODES + ctx->alloc_node_count;
}
size_t idx = ctx->free_node_start;
for (; idx < count; idx++) {
struct virt_addr_node *node = get_node_idx(ctx, idx);
if (!node->is_used) {
node->is_used = true;
ctx->used_node_count++;
return node;
}
}
panic("could not get virtaddr node");
}
static void free_node(struct virt_ctx *ctx, struct virt_addr_node *node)
{
node->is_used = false;
ctx->used_node_count--;
}
void virtaddr_init(struct virt_ctx *ctx)
{
struct virt_addr_node init = {
.start = 0x0,
.end = 0x1000000000000, // 48bit memory address max
.next = NULL,
.prev = NULL,
.is_alloc = false,
.is_used = true,
};
memset(ctx, 0, sizeof(struct virt_ctx));
ctx->bootstrap_nodes[0] = init;
ctx->alloc_nodes = NULL;
ctx->start_node = &ctx->bootstrap_nodes[0];
ctx->free_node_start = 0;
ctx->alloc_node_count = 0;
ctx->used_node_count = 0;
ctx->is_allocating = false;
virtaddr_take(ctx, (void *)0,
((uint64_t)kernel_end + PAGE_SIZE - 1) / PAGE_SIZE *
PAGE_SIZE);
}
int virtaddr_clone(struct virt_ctx *old, struct virt_ctx *new)
{
// copy over data
memcpy(new, old, sizeof(struct virt_ctx));
// allocate new space
new->alloc_nodes =
kalloc(sizeof(struct virt_addr_node) * new->alloc_node_count);
if (new->alloc_nodes == NULL)
return 1;
// update prev/next in new allocation space
update_node_ptrs(old->alloc_nodes, new->alloc_nodes, old->alloc_node_count,
new->alloc_node_count);
// update bootstrap nodes
for (size_t i = 0; i < new->used_node_count; i++) {
struct virt_addr_node *prev, *next;
if (i >= BOOTSTRAP_VIRT_ALLOC_NODES)
break;
// get prev
prev = i > 0 ? &new->bootstrap_nodes[i - 1] : NULL;
next = i < BOOTSTRAP_VIRT_ALLOC_NODES - 1 ?
&new->bootstrap_nodes[i + 1] :
NULL;
new->bootstrap_nodes[i].prev = prev;
new->bootstrap_nodes[i].next = next;
}
// get starting node
new->start_node = &new->bootstrap_nodes[0]; // for now
return 0;
}
static void merge_back(struct virt_ctx *ctx, struct virt_addr_node *node)
{
while (node->prev) {
if (node->is_alloc != node->prev->is_alloc)
break;
struct virt_addr_node *temp = node->prev;
node->start = temp->start;
node->prev = temp->prev;
if (temp->prev)
temp->prev->next = node;
free_node(ctx, temp);
}
if (node->prev == NULL) {
ctx->start_node = node;
}
}
static void merge_forward(struct virt_ctx *ctx, struct virt_addr_node *node)
{
while (node->next) {
if (node->is_alloc != node->next->is_alloc)
break;
struct virt_addr_node *temp = node->next;
node->end = temp->end;
node->next = temp->next;
if (temp->next)
temp->next->prev = node;
free_node(ctx, temp);
}
}
void *virtaddr_alloc(struct virt_ctx *ctx, int n_pages)
{
if (n_pages < 1)
return NULL;
long n_length = n_pages * PAGE_SIZE;
struct virt_addr_node *node = ctx->start_node;
for (; node != NULL; node = node->next) {
long length = node->end - node->start;
if (node->is_alloc)
continue;
if (length < n_length)
continue;
return (void *)node->start;
}
return NULL;
}
int virtaddr_take(struct virt_ctx *ctx, const void *virt, int n_pages)
{
if (n_pages < 1)
return 0;
long n_length = n_pages * PAGE_SIZE;
struct virt_addr_node *node = ctx->start_node;
for (; node != NULL; node = node->next) {
if (node->is_alloc)
continue;
if (node->start > (uintptr_t)virt ||
node->end < (uintptr_t)virt + n_length)
continue;
// create new node on left
if (node->start < (uintptr_t)virt) {
struct virt_addr_node *left = get_node(ctx);
left->next = node;
left->prev = node->prev;
left->start = node->start;
left->end = (uintptr_t)virt;
left->is_alloc = false;
if (node->prev)
node->prev->next = left;
node->prev = left;
}
// create new node on right
if (node->end > (uintptr_t)virt + n_length) {
struct virt_addr_node *right = get_node(ctx);
right->prev = node;
right->next = node->next;
right->start = (uintptr_t)virt + n_length;
right->end = node->end;
right->is_alloc = false;
if (node->next)
node->next->prev = right;
node->next = right;
}
node->start = (uintptr_t)virt;
node->end = node->start + n_length;
node->is_alloc = true;
return 0;
}
return 1;
}
long virtaddr_free(struct virt_ctx *ctx, const void *virtaddr)
{
if (virtaddr == NULL)
return -1;
uintptr_t virt = (uintptr_t)virtaddr;
if (virt % PAGE_SIZE)
return -1; // not page aligned, we did not give this out!!!
struct virt_addr_node *node = ctx->start_node;
for (; node != NULL; node = node->next) {
if (node->start == virt) {
int length = node->end - node->start;
int pages = length / PAGE_SIZE;
// FIXME: ???
node->is_alloc = false;
merge_back(ctx, node);
merge_forward(ctx, node);
return pages;
}
}
return -1;
}
void virtaddr_cleanup(struct virt_ctx *ctx)
{
kfree(ctx->alloc_nodes);
}
|