aboutsummaryrefslogtreecommitdiff
path: root/common/vmem.c
blob: c30f99b3fdad06c47262831ee8bd71174b00066d (plain) (blame)
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
#include <apos/vmem.h>
#include <apos/pmem.h>
#include <apos/mem.h>
#include <apos/string.h>

/* new idea, not implemented:
 * region(free) -> region(not free) -> region(free) -> region(free) ...
 *
 * each region comes right after the next, would at least be pretty quick to 
 * merge free blocks?
 */

enum mm_block_status_t {
	FREE, USED
};

struct mm_block_region_t;

/* linked list for now because I'm shit at coding */
struct mm_block_t {
	enum mm_block_status_t status;
	vm_t start;
	vm_t end;
	struct mm_block_t *next;
	struct mm_block_t *prev;
};

struct mm_block_region_t {
	vm_t vaddr;
	size_t blocks;
	size_t max_blocks;
	struct mm_block_region_t *next;
	struct mm_block_t *first;
};

static struct mm_block_region_t *root_region = 0;
/* a block represents free regions */
static struct mm_block_t *root_block = 0;

#define NODE_REGION(x) ((struct mm_block_region_t *)(((vm_t)x) & (__mm_page_shift - 1)))

static struct mm_block_t *get_free_block(struct vm_branch_t *branch)
{
	struct mm_block_region_t *region = root_region;
	size_t region_counter = 1;
	for(;region; region = region->next){
		if(region->next == 0){
			pm_t next_pa = alloc_page(MM_O0, 0);
			struct mm_block_region_t *next_va =
				(struct mm_block_region_t *)
				(region_counter * __o_size(MM_O0));

			map_vmem(branch, next_pa, (vm_t)next_va,
					VM_W | VM_R | VM_V, MM_O0);

			memset(next_va, 0, __o_size(MM_O0));
			next_va->max_blocks =
				(__o_size(MM_O0) - sizeof(struct mm_block_region_t))
				/ sizeof(struct mm_block_t);

			region->next = next_va;
		}

		if(region->blocks == region->max_blocks)
			continue;

		struct mm_block_t *block = region->first;
		for(size_t i = 0; i < region->max_blocks; ++i){
			if(block[i].start == 0 && block[i].end == 0){
				NODE_REGION(&block[i])->blocks++;
				return &block[i];
			}
		}
	}

	return 0;
}

void init_vmem(struct vm_branch_t *branch)
{
	pm_t first_block = alloc_page(MM_O0, 0);
	map_vmem(branch, first_block, 0, VM_W | VM_R | VM_V, MM_O0);
	memset(root_region, 0, __o_size(MM_O0));

	root_region->max_blocks = (__o_size(MM_O0) - sizeof(struct mm_block_region_t))
		/ sizeof(struct mm_block_t);
	root_region->first = (struct mm_block_t *)sizeof(struct mm_block_t);
	root_block = root_region->first;

	root_block->start = __o_size(MM_O0);
	/* TODO: add in UMEM_TOP or something */
	root_block->end = -1;
	root_block->status = USED;
}


/* ... new_node -> node ... */
static void insert_before(struct vm_branch_t *branch, struct mm_block_t *node, vm_t split)
{
	struct mm_block_t *new_node = get_free_block(branch);

	new_node->start = node->start;
	new_node->end = split;

	node->start = split;

	struct mm_block_t *prev = node->prev;
	new_node->next = node;
	new_node->prev = prev;
	prev->next = new_node;
	node->prev = new_node;
}

/* ... node -> new_node ... */
static void insert_after(struct vm_branch_t *branch, struct mm_block_t *node, vm_t split)
{
	struct mm_block_t *new_node = get_free_block(branch);

	new_node->start = split;
	new_node->end = node->end;

	node->end = split;

	struct mm_block_t *next = node->next;
	new_node->next = next;
	new_node->prev = node;
	next->prev = new_node;
	node->next = new_node;
}

static void gobble_block(struct vm_branch_t *branch, struct mm_block_t *node,
		vm_t start, vm_t end)
{
	node->status = USED;

	/* ... node ... */
	if(node->start == start && node->end == end)
		return;

	/* ... node -> new_node ... */
	if(node->start == start && node->end >= end){
		insert_after(branch, node, end);
		struct mm_block_t *new = node->next;

		new->status = FREE;
		return;
	}

	/* ... new_node -> node ...*/
	if(node->start < start && node->end == end){
		insert_before(branch, node, start);
		struct mm_block_t *new = node->prev;

		new->status = FREE;
		return;
	}

	/* ... new_node1 -> node -> new_node2 .. */
	insert_before(branch, node, start);
	struct mm_block_t *prev = node->prev;

	insert_after(branch, node, end);
	struct mm_block_t *next = node->next;

	prev->status = FREE;
	next->status = FREE;
}

/* only map with 4K blocks to keep it simple for now */
vm_t map_vregion(struct vm_branch_t *branch, pm_t base, vm_t start, size_t size,
		uint8_t flags)
{
	struct mm_block_t *node = root_block;
	for(; node; node = node->next){
		if(node->start > start)
			return 0;

		if(node->status == FREE && node->end >= start + size){
			gobble_block(branch, node, start, start + size);
			break;
		}
	}

	for(; size >= __o_size(MM_O0); size -= __o_size(MM_O0)){
		map_vmem(branch, start, base, flags, MM_O0);
		start += __o_size(MM_O0);
		base += __o_size(MM_O0);
	}

	return start;
}

static void free_block(struct mm_block_t *node)
{
	struct mm_block_t *prev = node->prev;
	struct mm_block_t *next = node->next;

	if(prev->status == FREE && next->status == FREE){
		/* merge all three blocks */
		prev->end = next->end;
		prev->next = next->next;
		next->next->prev = prev;

		node->start = 0;
		node->end = 0;

		next->start = 0;
		next->end = 0;

		NODE_REGION(node)->blocks--;
		NODE_REGION(next)->blocks--;
		return;
	}

	if(prev->status == FREE){
		prev->end = node->end;
		prev->next = next;
		next->prev = prev;

		node->start = 0;
		node->end = 0;

		NODE_REGION(node)->blocks--;
		return;
	}

	if(next->status == FREE){
		next->start = node->start;
		next->prev = prev;
		prev->next = next;

		node->start = 0;
		node->end = 0;

		NODE_REGION(node)->blocks--;
		return;
	}

	node->status = FREE;
}

void unmap_vregion(struct vm_branch_t *branch, vm_t start)
{
	size_t size = 0;
	struct mm_block_t *node = root_block;
	for(; node; node = node->next){
		/* if node->start == start status should be USED in all cases,
		 * but let's just go with this
		 */
		if(node->status == USED && node->start == start){
			size = node->end - node->start;
			free_block(node);
		}
	}

	for(; size >= __o_size(MM_O0); size -= __o_size(MM_O0)){
		unmap_vmem(branch, start, MM_O0);
		start += __o_size(MM_O0);
	}
}