-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_map.c
157 lines (130 loc) · 5.38 KB
/
memory_map.c
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
#include <stddef.h>
#include "memory_map.h"
#include "io.h"
#include "util.h"
#include "initramfs.h"
#define MMAP_SIZE 32
extern const uint32_t mboot_info;
extern char text_phys_begin[];
extern char bss_phys_end[];
void* init_ref;
void* init_ref_end;
size_t memory_map_size = 0;
struct memory_map_entry memory_map[MMAP_SIZE];
static struct memory_map_entry kernel;
static void cut_last_elem(struct memory_map_entry elem) {
if (memory_map[memory_map_size - 1].base_addr >= elem.base_addr) {
if (memory_map[memory_map_size - 1].length + memory_map[memory_map_size - 1].base_addr <= elem.base_addr + elem.length) {
--memory_map_size;
return;
}
if (memory_map[memory_map_size - 1].base_addr >= elem.base_addr + elem.length) {
return;
}
uint64_t end = memory_map[memory_map_size - 1].base_addr + memory_map[memory_map_size - 1].length;
memory_map[memory_map_size - 1].base_addr = elem.base_addr + elem.length;
memory_map[memory_map_size - 1].length = end - memory_map[memory_map_size - 1].base_addr;
} else {
if (elem.base_addr >= memory_map[memory_map_size - 1].base_addr + memory_map[memory_map_size - 1].length) {
return;
}
if (memory_map[memory_map_size - 1].length + memory_map[memory_map_size - 1].base_addr >= elem.base_addr + elem.length) {
uint64_t end = memory_map[memory_map_size - 1].base_addr + memory_map[memory_map_size - 1].length;
memory_map[memory_map_size - 1].length = elem.base_addr - memory_map[memory_map_size - 1].base_addr;
memory_map[memory_map_size].base_addr = elem.base_addr + elem.length;
memory_map[memory_map_size].length = end - memory_map[memory_map_size].base_addr;
memory_map[memory_map_size].type = memory_map[memory_map_size - 1].type;
++memory_map_size;
return;
}
memory_map[memory_map_size - 1].length = elem.base_addr - memory_map[memory_map_size].base_addr;
}
}
static void sort_mmap() {
for (size_t i = 0; i < memory_map_size; ++i) {
for (size_t j = i + 1; j < memory_map_size; ++j) {
if (memory_map[i].base_addr >= memory_map[j].base_addr) {
struct memory_map_entry k = memory_map[i];
memory_map[i] = memory_map[j];
memory_map[j] = k;
}
}
}
}
void find_initranfs_mode(uint32_t count, uint32_t* addr, uint32_t* start, uint32_t* end) {
for (uint32_t i = 0; i < count; ++i) {
uint32_t* pos = addr + i * 4;
char* mod_start = (char *) (uint64_t)*pos, *mod_end = (char *) (uint64_t)*(pos + 1);
if (mod_end - mod_start > 6) {
mod_start[0] = '0';
mod_start[1] = '7';
mod_start[2] = '0';
mod_start[3] = '7';
mod_start[4] = '0';
mod_start[5] = '1';
*start = (uint32_t)(uint64_t) mod_start;
*end = (uint32_t)(uint64_t) mod_end;
init_ref = mod_start;
init_ref_end = mod_end;
return;
}
}
}
void get_memory_map(){
memory_map_size = 0;
kernel.base_addr = (uint64_t)text_phys_begin;
kernel.length = (uint64_t)bss_phys_end - kernel.base_addr;
kernel.type = 0;
/*
* https://www.gnu.org/software/grub/manual/multiboot/multiboot.html
* 3.3 Boot information format
*/
uint32_t flags = *(uint32_t *) (uint64_t) mboot_info;
if (!((flags >> 6) & 1)) { //if flags[6] is not set then mmap_length and mmap_addr not present
puts("NO MMAP PROVIDED");
hang();
}
uint32_t mmap_length = *(uint32_t *) (uint64_t) (mboot_info + 44);
uint32_t mmap_addr = *(uint32_t *) (uint64_t) (mboot_info + 48);
uint32_t mods_count;
uint32_t mods_addr;
uint32_t start_del = 0, end_del = 0;
struct memory_map_entry initramfs;
if (((flags >> 3) & 1)) {
mods_addr = *(uint32_t *) (uint64_t) (mboot_info + 24);
mods_count = *(uint32_t *) (uint64_t) (mboot_info + 20);
find_initranfs_mode(mods_count, (uint32_t*)(uint64_t) mods_addr, &start_del, &end_del);
}
initramfs.base_addr = start_del;
initramfs.length = end_del - start_del;
initramfs.type = 0;
//Went in MEMORY MAP
uint32_t cnt_skip = 0;
while (cnt_skip < mmap_length) {
uint32_t size = *(uint32_t *)(uint64_t) (mmap_addr + cnt_skip);
cnt_skip += 4;
memory_map[memory_map_size].base_addr =*(uint64_t*)(uint64_t)(mmap_addr + cnt_skip);
memory_map[memory_map_size].length = *(uint64_t*)(uint64_t)(mmap_addr + cnt_skip + 8);
memory_map[memory_map_size].type = *(uint32_t*)(uint64_t)(mmap_addr + cnt_skip + 16);
cnt_skip += size;
++memory_map_size;
cut_last_elem(kernel);
if (end_del - start_del > 0) {
cut_last_elem(initramfs);
}
}
memory_map[memory_map_size] = kernel;
++memory_map_size;
if (initramfs.length > 0) {
memory_map[memory_map_size] = initramfs;
++memory_map_size;
}
sort_mmap();
}
void print_mempry_map() {
for (size_t i = 0; i < memory_map_size; ++i) {
printf("%llx - %llx, %s\n", (unsigned long long) memory_map[i].base_addr,
(unsigned long long int) (memory_map[i].base_addr + memory_map[i].length - 1),
memory_map[i].type == 1 ? "Available" : memory_map[i].type == 0 ? "Kernel" : "Reserved");
}
}