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
|
#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>
typedef struct FreePage FreePage;
struct FreePage {
FreePage *next;
};
static FreePage *firstfreepage;
extern uint8_t pbitmap[]; /* linker.ld */
static size_t pbitmap_len; /* in bytes */
static void *watermark; /* first nonallocated page */
static void *memtop; /* end of physical memory */
static int curallocs = 0;
static size_t
toindex(void *p)
{
assert((void*)pbitmap <= p);
return ((uintptr_t)p - (uintptr_t)pbitmap) / PAGE_SIZE;
}
static bool
pbitmap_get(void *p)
{
size_t i = toindex(p);
size_t b = i / 8;
uint8_t m = 1 << (i&7);
assert(b < pbitmap_len);
return (pbitmap[b]&m) != 0;
}
static bool
pbitmap_set(void *p, bool v)
{
size_t i = toindex(p);
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;
}
/* watermark allocator. doesn't mark the page as allocated in the bitmap. */
static void *
wmalloc(void)
{
/* skip over reserved pages */
while (watermark < memtop && pbitmap_get(watermark)) {
watermark += PAGE_SIZE;
}
if (watermark < memtop) {
void *p = watermark;
watermark += PAGE_SIZE;
return p;
} else {
return NULL;
}
}
void
mem_init(void *p)
{
memtop = p;
kprintf("memory %8x -> %8x\n", &_bss_end, memtop);
pbitmap_len = (toindex(memtop) + 7) / 8; /* rounds up */
memset(pbitmap, 0, pbitmap_len);
mem_reserve(pbitmap, pbitmap_len);
mem_reserve(memtop, 4096 * 8);
watermark = pbitmap;
}
void
mem_reserve(void *addr, size_t len)
{
kprintf("reserved %8x -> %8x\n", addr, addr + len);
void *top = min(addr + len, memtop);
addr = (void*)((uintptr_t)addr & ~PAGE_MASK); /* round down to page */
for (void *p = max(addr, (void*)pbitmap); p < top; p += PAGE_SIZE) {
/* This doesn't allow overlapping reserved regions, but, more
* importantly, it prevents reserving an already allocated page.
* Note that a reserved page can be freed. */
if (pbitmap_get(p)) {
panic_invalid_state();
}
pbitmap_set(p, true);
curallocs++;
}
}
void *
page_zalloc(size_t pages)
{
void *p = page_alloc(pages);
memset(p, 0, pages * PAGE_SIZE);
return p;
}
void *
page_alloc(size_t pages)
{
void *ret;
assert(pages == 1);
if (firstfreepage != NULL) {
ret = firstfreepage;
firstfreepage = firstfreepage->next;
} else {
ret = wmalloc();
}
if (ret == NULL) {
mem_debugprint();
kprintf("we ran out of memory :(\ngoodbye.\n");
panic_unimplemented();
} else {
assert(pbitmap_get(ret) == false);
pbitmap_set(ret, true);
curallocs += pages;
return ret;
}
}
void
page_free(void *addr, size_t pages)
{
assert((void*)pbitmap <= addr);
for (size_t i = 0; i < pages; i++) {
FreePage *fp = addr + i*PAGE_SIZE;
assert(pbitmap_get(fp) == true);
pbitmap_set(fp, false);
fp->next = firstfreepage;
firstfreepage = fp;
}
curallocs -= pages;
}
void
mem_debugprint(void)
{
kprintf(
"[kern] %d pages allocated, watermark at %08p / %08p\n",
curallocs, watermark, memtop
);
kmalloc_debugprint();
}
|