This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
/
physical.c
executable file
·254 lines (238 loc) · 8.23 KB
/
physical.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
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
/*
This file is part of VK.
Copyright (C) 2017 Valentin Haudiquet
VK is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 2.
VK is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VK. If not, see <http://www.gnu.org/licenses/>.
*/
#include "system.h"
#include "mem.h"
#include "error/error.h"
/*
* This file has the goal to trace the physical memory and to gets avaible blocks of it
*/
p_block_t* first_block = 0;
u64 detected_memory = 0;
u32 detected_memory_below32;
void physmem_get(multiboot_info_t* mbt)
{
//Parse memory map from GRUB
memory_map_t* mmap = (memory_map_t*) (mbt->mmap_addr+KERNEL_VIRTUAL_BASE);
p_block_t* prev = 0;
p_block_t* current_block = first_block =
#ifdef MEMLEAK_DBG
kmalloc(sizeof(p_block_t), "Physical memory block struct");
#else
kmalloc(sizeof(p_block_t));
#endif
unsigned int i = 0;
while(i < mbt->mmap_length)
{
if(first_block == 0) first_block = current_block;
detected_memory += mmap->length;
if(mmap->base_addr < U32_MAX)
{
current_block->base_addr = (u32) mmap->base_addr;
if(mmap->length < U32_MAX)
current_block->size = (u32) mmap->length;
else
current_block->size = U32_MAX-1;
detected_memory_below32 += current_block->size;
current_block->type = mmap->type;
current_block->prev = prev;
i+= (sizeof(memory_map_t));
if(i < mbt->mmap_length)
{
current_block->next =
#ifdef MEMLEAK_DBG
kmalloc(sizeof(p_block_t), "Physical memory block");
#else
kmalloc(sizeof(p_block_t));
#endif
prev = current_block;
current_block = current_block->next;
mmap = (memory_map_t*) ((u32) mmap + mmap->size + sizeof(mmap->size));
}
}
else
{
i+= (sizeof(memory_map_t));
if(i < mbt->mmap_length)
mmap = (memory_map_t*) ((u32) mmap + mmap->size + sizeof(mmap->size));
}
}
//Mark the kernel page as used (except the first 1 mib that are mapped but free/used by hardware)
reserve_specific(0x100000, 0x300000, PHYS_KERNEL_BLOCK_TYPE);
//Mark the kernel heap page as used
reserve_specific(KHEAP_PHYS_START, KHEAP_BASE_SIZE, PHYS_KERNEL_BLOCK_TYPE);
}
u32 get_free_mem()
{
u32 tr = 0;
p_block_t* curr = first_block;
while(curr)
{
if(curr->type == 1 && curr->base_addr > 0x100000) tr+= curr->size;
curr = curr->next;
}
return tr;
}
u32 reserve_block(u32 size, u8 type)
{
alignup(size, 4096);
p_block_t* curr = first_block;
while(curr)
{
if(curr->base_addr >= 0x100000 && curr->type == PHYS_FREE_BLOCK_TYPE)
{
if(curr->size < size) {curr = curr->next; continue;}
p_block_t* newblock =
#ifdef MEMLEAK_DBG
kmalloc(sizeof(p_block_t), "physical memory new block (reserve_block)");
#else
kmalloc(sizeof(p_block_t));
#endif
p_block_t* next = curr->next;
newblock->base_addr = curr->base_addr+size;
newblock->size = curr->size-size;
newblock->type = 1;
newblock->next = next;
newblock->prev = curr;
if(next) next->prev = newblock;
curr->size = size;
curr->next = newblock;
curr->type = type;
return curr->base_addr;
}
curr = curr->next;
}
fatal_kernel_error("Trying to reserve more physical memory than available", "RESERVE_BLOCK");
return 0;
}
//CARE : UNSAFE
u32 reserve_specific(u32 addr, u32 size, u8 type)
{
p_block_t* curr = first_block;
while(curr)
{
if(curr->base_addr == addr && curr->type == PHYS_FREE_BLOCK_TYPE)
{
if(curr->type != PHYS_FREE_BLOCK_TYPE) break;
if(curr->size < size) {break;}
p_block_t* newblock =
#ifdef MEMLEAK_DBG
kmalloc(sizeof(p_block_t), "physical memory new block (reserve_specific)");
#else
kmalloc(sizeof(p_block_t));
#endif
p_block_t* next = curr->next;
newblock->base_addr = curr->base_addr+size;
newblock->size = curr->size-size;
newblock->type = PHYS_FREE_BLOCK_TYPE;
newblock->next = next;
newblock->prev = curr;
next->prev = newblock;
curr->size = size;
curr->next = newblock;
curr->type = type;
return curr->base_addr;
}
else if(curr->base_addr < addr && curr->base_addr+curr->size > addr)
{
if(curr->type != PHYS_FREE_BLOCK_TYPE) break;
if(curr->size < size) {break;}
p_block_t* beforeblock =
#ifdef MEMLEAK_DBG
kmalloc(sizeof(p_block_t), "physical memory new block (reserve_specific)");
#else
kmalloc(sizeof(p_block_t));
#endif
beforeblock->base_addr = curr->base_addr;
beforeblock->size = addr - size;
beforeblock->next = curr;
beforeblock->prev = curr->prev;
beforeblock->type = PHYS_FREE_BLOCK_TYPE;
curr->prev->next = beforeblock;
curr->prev = beforeblock;
curr->base_addr = addr;
curr->size -= beforeblock->size;
p_block_t* afterblock =
#ifdef MEMLEAK_DBG
kmalloc(sizeof(p_block_t), "physical memory new block (reserve_specific)");
#else
kmalloc(sizeof(p_block_t));
#endif
p_block_t* next = curr->next;
afterblock->base_addr = curr->base_addr+size;
afterblock->size = curr->size-size;
afterblock->type = PHYS_FREE_BLOCK_TYPE;
afterblock->next = next;
afterblock->prev = curr;
curr->next = afterblock;
curr->size = size;
curr->type = type;
return curr->base_addr;
}
curr = curr->next;
}
fatal_kernel_error("Trying to reserve specific failed", "RESERVE_SPECIFIC");
return 0;
}
void free_block(u32 base_addr)
{
p_block_t* curr = first_block;
while(curr)
{
if(curr->base_addr == base_addr)
{
if((curr->type != PHYS_KERNELF_BLOCK_TYPE) && (curr->type != PHYS_USER_BLOCK_TYPE))
{
kprintf("%lPHYSICAL FREE_BLOCK() ERROR\n", 2);
kprintf("Block address (physical) : 0x%X\n", curr->base_addr);
fatal_kernel_error("Trying to free a non-freeable block", "FREE_BLOCK");
}
curr->type = PHYS_FREE_BLOCK_TYPE;
//block merging before and after
p_block_t* m = curr->prev;
while(m && m->type == PHYS_FREE_BLOCK_TYPE)
{
m->size += curr->size;
m->next = curr->next;
if(m->next) m->next->prev = m;
kfree(curr);
curr = m;
m = m->prev;
}
m = curr->next;
while(m && m->type == PHYS_FREE_BLOCK_TYPE)
{
curr->size += m->size;
curr->next = m->next;
if(curr->next) curr->next->prev = curr;
kfree(m);
m = curr->next;
}
return;
}
curr = curr->next;
}
fatal_kernel_error("Trying to free an unknown block", "FREE_BLOCK");
}
p_block_t* get_block(u32 some_addr)
{
p_block_t* curr = first_block;
while(curr)
{
if(curr->base_addr < some_addr && curr->base_addr+curr->size > some_addr)
return curr;
curr = curr->next;
}
fatal_kernel_error("This address is not present in memory", "GET_BLOCK");
return 0;
}