summaryrefslogtreecommitdiff
path: root/src/kernel/malloc.c
blob: ba1ab42d3a59b25d2674732b82a86e684618bc45 (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
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
#include <kernel/arch/generic.h>
#include <kernel/malloc.h>
#include <kernel/panic.h>
#include <kernel/util.h>
#include <shared/mem.h>
#include <stdbool.h>
#include <stdint.h>

#define MALLOC_MAGIC 0x616c6c6f63686472
#define DESCLEN 8

typedef struct Allocation Allocation;
struct Allocation {
	uint64_t magic;
	uint64_t len;
	Allocation *next, *prev;
	void *stacktrace[4];
	char desc[DESCLEN];
};

Allocation *malloc_last = NULL;

extern uint8_t pbitmap[]; /* linker.ld */
static size_t pbitmap_len; /* in bytes */
static size_t pbitmap_firstfree = 0;


static
bool bitmap_get(long i)
{
	assert(i >= 0);
	size_t b = i / 8;
	uint8_t m = 1 << (i&7);
	assert(b < pbitmap_len);
	return (pbitmap[b]&m) != 0;
}

static
bool bitmap_set(long i, bool v)
{
	assert(i >= 0);
	size_t b = i / 8;
	uint8_t m = 1 << (i&7);
	assert(b < pbitmap_len);
	bool prev = (pbitmap[b]&m) != 0;
	if (v) pbitmap[b] |=  m;
	else   pbitmap[b] &= ~m;
	return prev;
}

static
long toindex(void *p)
{
	return ((long)p - (long)pbitmap) / PAGE_SIZE;
}

static
size_t page_amt(size_t bytes)
{
	return (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
}

void
mem_init(void *memtop)
{
	kprintf("memory   %8x -> %8x\n", &_bss_end, memtop);
	pbitmap_len = toindex(memtop) / 8;
	memset(pbitmap, 0, pbitmap_len);
	mem_reserve(pbitmap, pbitmap_len);
}

void
mem_reserve(void *addr, size_t len)
{
	kprintf("reserved %8x -> %8x\n", addr, addr + len);

	/* align to the previous page */
	size_t off = (uintptr_t)addr & PAGE_MASK;
	addr -= off;
	len += off;
	size_t first = toindex(addr);
	for (size_t i = 0; i * PAGE_SIZE < len; i++) {
		if ((first + i) / 8 >= pbitmap_len)
			break;
		bitmap_set(first + i, true);
	}
}

void
mem_debugprint(void)
{
	size_t count = 0, bytes = 0, pages = 0;
	kprintf("[kern] current allocations:\n");
	for (Allocation *iter = malloc_last; iter; iter = iter->prev) {
		kprintf(
			"%08p %6dB %.8s ",
			((void*)iter) + sizeof(Allocation),
			iter->len - sizeof(Allocation),
			iter->desc
		);
		for (size_t i = 0; i < 4; i++) {
			kprintf(" k/%08x", iter->stacktrace[i]);
		}
		kprintf("\n");

		count++;
		bytes += iter->len;
		pages += page_amt(iter->len);
	}
	kprintf(
		"%d in total, %d bytes, %d pages = %dB used\n",
		count, bytes, pages, pages*PAGE_SIZE
	);
}

void
*page_alloc(size_t pages)
{
	assert(pages == 1); /* not using that assertion... yet */
	/* i do realize how painfully slow this is */
	size_t streak = 0;
	for (size_t i = pbitmap_firstfree; i < pbitmap_len * 8; i++) {
		if (bitmap_get(i)) {
			streak = 0;
			continue;
		}
		if (++streak >= pages) {
			/* found hole big enough for this allocation */
			i = i + 1 - streak;
			for (size_t j = 0; j < streak; j++)
				bitmap_set(i + j, true);
			pbitmap_firstfree = i + streak - 1;
			return pbitmap + i * PAGE_SIZE;
		}
	}
	kprintf("we ran out of memory :(\ngoodbye.\n");
	panic_unimplemented();
}

void
*page_zalloc(size_t pages)
{
	void *p = page_alloc(pages);
	memset(p, 0, pages * PAGE_SIZE);
	return p;
}

/* frees `pages` consecutive pages starting from *first */
void
page_free(void *addr, size_t pages)
{
	assert(addr >= (void*)pbitmap);
	size_t first = toindex(addr);
	for (size_t i = 0; i < pages; i++) {
		if (bitmap_set(first + i, false) == false) {
			panic_invalid_state();
		}
	}
	if (pbitmap_firstfree > first) {
		pbitmap_firstfree = first;
	}
}

void
kmalloc_sanity(const void *addr)
{
	assert(addr);
	const Allocation *hdr = addr - sizeof(Allocation);
	assert(hdr->magic == MALLOC_MAGIC);
	if (hdr->next) assert(hdr->next->prev == hdr);
	if (hdr->prev) assert(hdr->prev->next == hdr);
}

// TODO better kmalloc
void
*kmalloc(size_t len, const char *desc)
{
	Allocation *hdr;
	void *addr;

	if (KMALLOC_MAX < len) {
		panic_invalid_state();
	}
	len += sizeof(Allocation);
	assert(len <= PAGE_SIZE);
	hdr = page_alloc(page_amt(len));
	hdr->magic = MALLOC_MAGIC;
	hdr->len = len;

	memset(hdr->desc, ' ', DESCLEN);
	if (desc) {
		for (int i = 0; i < DESCLEN; i++) {
			if (desc[i] == '\0') break;
			hdr->desc[i] = desc[i];
		}
	}

	hdr->next = NULL;
	hdr->prev = malloc_last;
	if (hdr->prev) {
		assert(!hdr->prev->next);
		hdr->prev->next = hdr;
	}

	for (size_t i = 0; i < 4; i++)
		hdr->stacktrace[i] = debug_caller(i);

	malloc_last = hdr;

	addr = (void*)hdr + sizeof(Allocation);
#ifndef NDEBUG
	memset(addr, 0xCC, len);
#endif
	kmalloc_sanity(addr);
	return addr;
}

void
kfree(void *ptr)
{
	Allocation *hdr;
	size_t pages;
	if (ptr == NULL) return;

	hdr = ptr - sizeof(Allocation);
	kmalloc_sanity(ptr);
	pages = page_amt(hdr->len);

	hdr->magic = ~MALLOC_MAGIC; // (hopefully) detect double frees
	if (hdr->next)
		hdr->next->prev = hdr->prev;
	if (hdr->prev)
		hdr->prev->next = hdr->next;
	if (malloc_last == hdr)
		malloc_last = hdr->prev;
#ifndef NDEBUG
	memset(hdr, 0xC0, pages * PAGE_SIZE);
#endif
	page_free(hdr, pages);
}