Skip to content
Snippets Groups Projects
Commit 934229df authored by Bensong Liu's avatar Bensong Liu
Browse files

working on kalloc

parent 2918294e
No related branches found
No related tags found
No related merge requests found
Pipeline #833 passed with stage
in 2 minutes and 55 seconds
...@@ -28,6 +28,8 @@ Just used this mode to launch 64bit long mode. No paging enabled. ...@@ -28,6 +28,8 @@ Just used this mode to launch 64bit long mode. No paging enabled.
0x1000 - 0x5000 : 64bit long mode paging table**s** 0x1000 - 0x5000 : 64bit long mode paging table**s**
0x00000000 - 0x00200000 : 512 * 4K pages, before the kernel initializing page table in C++. 0x00000000 - 0x00200000 : 512 * 4K pages, before the kernel initializing page table in C++.
(0x0 - 0x100000 are used by kernel structures, 0x100000-0x200000 are used by kalloc(), defined in kutils.hpp)
## bootloader(legacy) disk model ## bootloader(legacy) disk model
0-511Byte : bootloader.img from boot.asm, MBR flag 0-511Byte : bootloader.img from boot.asm, MBR flag
......
#ifndef ROS_KUTILS_HPP
#define ROS_KUTILS_HPP
#include "stdint.hpp"
#include "stdlib.hpp"
namespace impl {
constexpr uint64_t kalloc_begin_addr = 0x100000;
constexpr uint64_t kalloc_end_addr = 0x200000;
// Currently 1MB, must be paged.
struct __attribute__((packed)) free_slot_head_t {
uint32_t size;
uint64_t next; // 0x0 means tail.
};
static_assert(sizeof(free_slot_head_t) == sizeof(uint32_t) + sizeof(uint64_t));
using allocated_slot_head_t = uint32_t; // contains size EXCEPT the head!
inline void kalloc_init_pool() {
struct __attribute__((packed)) _kalloc_head {
uint32_t magic = 0x12230823;
free_slot_head_t free_slot_head {kalloc_end_addr - kalloc_begin_addr - sizeof(_kalloc_head), 0x0};
} kalloc_head;
static_assert(sizeof(kalloc_head) == sizeof(uint32_t)*2 + sizeof(uint64_t));
memcpy((char *)kalloc_begin_addr, (const char *)&kalloc_head, sizeof(kalloc_head));
}
}
inline void *kalloc(const uint32_t requested_size) {
const auto req_size_with_head = requested_size + sizeof(impl::allocated_slot_head_t);
uint64_t prev_slot_addr = 0x0;
auto curr_slot_addr = impl::kalloc_begin_addr + sizeof(impl::free_slot_head_t::size);
while(true) {
auto curr_slot = (impl::free_slot_head_t *)curr_slot_addr;
if(curr_slot->size >= req_size_with_head) {
// Do the alloc
curr_slot->size -= req_size_with_head;
if(curr_slot->size < 32) {
// Give the whole slot
if(prev_slot_addr == 0x0) {
// No previous slot. I am the first one.
// TODO
}
else {
auto prev_slot = (impl::free_slot_head_t *)prev_slot_addr;
prev_slot->next = curr_slot->next;
}
const auto actually_assigned_size = sizeof(impl::free_slot_head_t) + curr_slot->size;
*(impl::allocated_slot_head_t *)curr_slot_addr = actually_assigned_size;
return (char *)curr_slot_addr + sizeof(impl::allocated_slot_head_t);
}
else {
// Shrink the slot.
// TODO
}
}
else if (curr_slot->next != 0x0){
// Failed. Next.
prev_slot_addr = curr_slot_addr;
curr_slot_addr = curr_slot->next;
}
else {
// No memory
return 0x0;
}
}
}
inline void kfree(void *memory) {
}
#endif
...@@ -47,4 +47,14 @@ inline void print(const char *cstr, uint8_t color = default_color) { ...@@ -47,4 +47,14 @@ inline void print(const char *cstr, uint8_t color = default_color) {
} }
} }
#endif inline void clear_screen() {
\ No newline at end of file for(auto y = 0; y < 25; ++y) {
for(auto x = 0; x < 80; ++x) {
char c = x%10 + '0';
char color = x + y*80;
set_char(x, y, ' ', 0x0f);
}
}
}
#endif
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "include/vga.hpp" #include "include/vga.hpp"
#include "include/bus_io.hpp" #include "include/bus_io.hpp"
#include "include/kutils.hpp"
void test_dummy_function() { void test_dummy_function() {
auto tmp = read_byte_from_bus(0x3f2); auto tmp = read_byte_from_bus(0x3f2);
...@@ -9,14 +10,12 @@ void test_dummy_function() { ...@@ -9,14 +10,12 @@ void test_dummy_function() {
write_byte_to_bus(0x3f2, tmp); write_byte_to_bus(0x3f2, tmp);
} }
void kernel_init() {
impl::kalloc_init_pool();
}
void main() { void main() {
for(auto y = 0; y < 25; ++y) { clear_screen();
for(auto x = 0; x < 80; ++x) {
char c = x%10 + '0';
char color = x + y*80;
set_char(x, y, ' ', 0x0f);
}
}
print("Hello world!\n"); print("Hello world!\n");
print("Hello world!\n", 0xb1); print("Hello world!\n", 0xb1);
print("Hello world!\n", 0xae); print("Hello world!\n", 0xae);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment