Skip to content

Commit

Permalink
Pass VmmInfo to vmm_init and vmm_alloc (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamos82 authored Aug 8, 2023
1 parent 5f37e8f commit dee5397
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 77 deletions.
4 changes: 2 additions & 2 deletions src/include/kernel/mem/vmm.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ extern uint64_t end_of_vmm_space;
extern VmmInfo vmm_info;
extern uintptr_t higherHalfDirectMapBase; /**< The start of the physical memory direct mapping */

void vmm_init(vmm_level_t vmm_level);
void vmm_init(vmm_level_t vmm_level, VmmInfo *vmm_info);

void *vmm_alloc(size_t size, size_t flags);
void *vmm_alloc(size_t size, size_t flags, VmmInfo *vmm_info);
void vmm_free(void *address);

void *map_vaddress(void *address, size_t flags);
Expand Down
5 changes: 4 additions & 1 deletion src/include/kernel/scheduling/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stddef.h>
#include <stdbool.h>
#include <thread.h>
#include <vmm.h>

#define TASK_NAME_MAX_LEN 32

Expand All @@ -16,7 +17,9 @@ struct task_t {
// It will contain the virtual memory base address for the process
void* vm_root_page_table;

//List of threads
VmmInfo vmm_data;

//List of threads
struct thread_t* threads;
task_t* parent;
task_t* next;
Expand Down
38 changes: 19 additions & 19 deletions src/kernel/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* main.c
* main.c
* Kernel entry point from bootloader
* */

Expand Down Expand Up @@ -64,7 +64,7 @@ void _init_basic_system(unsigned long addr){
tagmmap = (struct multiboot_tag_mmap *) (multiboot_mmap_data + _HIGHER_HALF_KERNEL_MEM_START);
tagfb = (struct multiboot_tag_framebuffer *) (multiboot_framebuffer_data + _HIGHER_HALF_KERNEL_MEM_START);
//Print basic mem Info data
loglinef(Verbose, "(kernel_main) init_basic_system: Found basic mem Mem info type: 0x%x", tagmem->type);
loglinef(Verbose, "(kernel_main) init_basic_system: Found basic mem Mem info type: 0x%x", tagmem->type);
loglinef(Verbose, "(kernel_main) init_basic_system: Memory lower (in kb): %d - upper (in kb): %d", tagmem->mem_lower, tagmem->mem_upper);
memory_size_in_bytes = (tagmem->mem_upper + 1024) * 1024;
//Print mmap_info
Expand All @@ -78,7 +78,7 @@ void _init_basic_system(unsigned long addr){
loglinef(Verbose, "(kernel_main) init_basic_system: width: 0x%x - height: 0x%x - bpp: 0x%x - pitch: 0x%x", tagfb->common.framebuffer_width, tagfb->common.framebuffer_height, tagfb->common.framebuffer_bpp, tagfb->common.framebuffer_pitch);
set_fb_data(tagfb);
loglinef(Verbose, "(kernel_main) init_basic_system: Total framebuffer size is: 0x%x", framebuffer_data.memory_size);

tagacpi = (struct multiboot_tag *) (multiboot_acpi_info + _HIGHER_HALF_KERNEL_MEM_START);
if(tagacpi->type == MULTIBOOT_TAG_TYPE_ACPI_OLD){
tagold_acpi = (struct multiboot_tag_old_acpi *)tagacpi;
Expand All @@ -93,10 +93,10 @@ void _init_basic_system(unsigned long addr){
parse_SDT((uint64_t) descriptor, MULTIBOOT_TAG_TYPE_ACPI_NEW);
validate_SDT((char *) descriptor, sizeof(RSDPDescriptor20));
}

for (tag=(struct multiboot_tag *) (addr + _HIGHER_HALF_KERNEL_MEM_START + 8);
tag->type != MULTIBOOT_TAG_TYPE_END;
tag = (struct multiboot_tag *) ((multiboot_uint8_t *) tag
tag = (struct multiboot_tag *) ((multiboot_uint8_t *) tag
+ ((tag->size + 7) & ~7))){
switch(tag->type){
default:
Expand All @@ -116,30 +116,30 @@ void kernel_start(unsigned long addr, unsigned long magic){
loglinef(Verbose, "(kernel_start): Kernel End: 0x%x - Physical: %x", (unsigned long)&_kernel_end, (unsigned long)&_kernel_physical_end);
// Reminder here: The first 8 bytes have a fixed structure in the multiboot info:
// They are: 0-4: size of the boot information in bytes
// 4-8: Reserved (0)
unsigned size = *(unsigned*)(addr + _HIGHER_HALF_KERNEL_MEM_START);
// 4-8: Reserved (0)
unsigned size = *(unsigned*)(addr + _HIGHER_HALF_KERNEL_MEM_START);

if(magic == 0x36d76289){
loglinef(Verbose, "(kernel_start): Magic number verified Size: %x - Magic: %x", size, magic);
} else {
logline(Verbose, "(kernel_start): Failed to verify magic number. Something is wrong");
}

#if USE_FRAMEBUFFER == 1

if(get_PSF_version(_binary_fonts_default_psf_start) == 1){
logline(Verbose, "(kernel_start): PSF v1 found");
PSFv1_Font *font = (PSFv1_Font*)_binary_fonts_default_psf_start;
loglinef(Verbose, "(kernel_start): PSF v1: Magic: [%x %x] - Flags: 0x%x - Charsize: 0x%x", font->magic[1], font->magic[0], font->mode, font->charsize);
loglinef(Verbose, "(kernel_start): PSF v1: Magic: [%x %x] - Flags: 0x%x - Charsize: 0x%x", font->magic[1], font->magic[0], font->mode, font->charsize);
} else {
PSF_font *font = (PSF_font*)&_binary_fonts_default_psf_start;
logline(Verbose, "(kernel_start): PSF v2 found");
loglinef(Verbose, "(kernel_start): Version: 0x%x - Magic: 0x%x", font->magic, font->version);
loglinef(Verbose, "(kernel_start): Number of glyphs: 0x%x - Bytes per glyphs: 0x%x", font->numglyph, font->bytesperglyph);
loglinef(Verbose, "(kernel_start): Header size: 0x%x - flags: 0x%x", font->headersize, font->flags);
loglinef(Verbose, "(kernel_start): Width: 0x%x - Height: 0x%x", font->width, font->height);
loglinef(Verbose, "(kernel_start): Width: 0x%x - Height: 0x%x", font->width, font->height);
}

loglinef(Verbose, "(kernel_start): PSF stored version: %d", psf_font_version);
uint32_t pw, ph, cw, ch;
get_framebuffer_mode(&pw, &ph, &cw, &ch);
Expand All @@ -152,24 +152,24 @@ void kernel_start(unsigned long addr, unsigned long magic){

draw_logo(0, 400);
#endif

/*char *cpuid_model = _cpuid_model();
loglinef(Verbose, "(kernel_start): Cpuid model: %s", cpuid_model);
uint32_t cpu_info = 0;
cpu_info = _cpuid_feature_apic();
loglinef(Verbose, "(kernel_start): Cpu info result: 0x%x", cpu_info);*/
init_apic();
_mmap_setup();
vmm_init(VMM_LEVEL_SUPERVISOR);
vmm_init(VMM_LEVEL_SUPERVISOR, NULL);
vmm_direct_map_physical_memory();
initialize_kheap();
kernel_settings.kernel_uptime = 0;
kernel_settings.paging.page_root_address = p4_table;
kernel_settings.paging.page_generation = 0;
//The table containing the IOAPIC information is called MADT
//The table containing the IOAPIC information is called MADT
MADT* madt_table = (MADT*) get_SDT_item(MADT_ID);
loglinef(Verbose, "(kernel_main) Madt SIGNATURE: %x - ADDRESS: %.4s", madt_table->header.Signature, madt_table);
loglinef(Verbose, "(kernel_main) Madt SIGNATURE: %x - ADDRESS: %.4s", madt_table->header.Signature, madt_table);
loglinef(Verbose, "(kernel_main) MADT local apic base: %x - Madt Length: %d", madt_table->local_apic_base, madt_table->header.Length);
print_madt_table(madt_table);
init_ioapic(madt_table);
Expand All @@ -180,14 +180,14 @@ void kernel_start(unsigned long addr, unsigned long magic){

uint32_t apic_ticks = calibrate_apic();
kernel_settings.apic_timer.timer_ticks_base = apic_ticks;
loglinef(Verbose, "(kernel_main) Calibrated apic value: %u", apic_ticks);
loglinef(Verbose, "(kernel_main) Calibrated apic value: %u", apic_ticks);
loglinef(Verbose, "(kernel_main) (END of Mapped memory: 0x%x)", end_of_mapped_memory);
vfs_init();
logline(Info, "(kernel_main) Init end!! Starting infinite loop");
uint64_t unix_timestamp = read_rtc_time();
#if USE_FRAMEBUFFER == 1
_fb_printStrAndNumber("Epoch time: ", unix_timestamp, 0, 5, 0xf5c4f1, 0x000000);
#endif
#endif
init_scheduler();
char a = 'a';
char b = 'b';
Expand Down
28 changes: 14 additions & 14 deletions src/kernel/mem/kheap.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
KHeapMemoryNode *kernel_heap_start;
KHeapMemoryNode *kernel_heap_current_pos;
KHeapMemoryNode *kernel_heap_end;

extern uint64_t end_of_mapped_memory;

void initialize_kheap(){
#ifndef _TEST_

// Let's allocate the new heap, we rely on the vmm_alloc function for this part.
uint64_t *kheap_vaddress = vmm_alloc(PAGE_SIZE_IN_BYTES, VMM_FLAGS_PRESENT | VMM_FLAGS_WRITE_ENABLE);
uint64_t *kheap_vaddress = vmm_alloc(PAGE_SIZE_IN_BYTES, VMM_FLAGS_PRESENT | VMM_FLAGS_WRITE_ENABLE, NULL);

kernel_heap_start = (KHeapMemoryNode *) ((uint64_t) kheap_vaddress);
loglinef(Verbose, "(initialize_kheap) Start address using vmm_alloc: %x, and using end of vmm_space: %x", kheap_vaddress, kernel_heap_start);
Expand All @@ -43,13 +43,13 @@ size_t align(size_t size) {

void *kmalloc(size_t size) {
KHeapMemoryNode *current_node = kernel_heap_start;
// If size is 0 we don't need to do anything
// If size is 0 we don't need to do anything
if( size == 0 ) {
loglinef(Verbose, "(kmalloc) Size is null");
return NULL;
}

//loglinef(Verbose, "(kmalloc) Current heap free size: 0x%x - Required: 0x%x", current_node->size, align(size + sizeof(KHeapMemoryNode)));
//loglinef(Verbose, "(kmalloc) Current heap free size: 0x%x - Required: 0x%x", current_node->size, align(size + sizeof(KHeapMemoryNode)));

while( current_node != NULL ) {
// The size of a node contains also the size of the header, so when creating nodes we add headers
Expand Down Expand Up @@ -81,7 +81,7 @@ void *kmalloc(size_t size) {
expand_heap(real_size);
if( current_node->prev != NULL) {
// If we are here it means that we were at the end of the heap and needed an expansion
// So after the expansion there are chances that we reach the end of the heap, and the
// So after the expansion there are chances that we reach the end of the heap, and the
// loop will end here. So let's move back of one item in the list, so we are sure the next item to be picked
// will be the new one.
current_node = current_node->prev;
Expand Down Expand Up @@ -118,7 +118,7 @@ void expand_heap(size_t required_size) {
if ( available_merges & MERGE_LEFT) {
merge_memory_nodes(new_tail->prev, new_tail);
}

}

uint64_t compute_kheap_end() {
Expand All @@ -142,16 +142,16 @@ void kfree(void *ptr) {
if( ((uint64_t) current_node + sizeof(KHeapMemoryNode)) == (uint64_t) ptr) {
current_node->is_free = true;
uint8_t available_merges = can_merge(current_node);

if( available_merges & MERGE_RIGHT ) {
merge_memory_nodes(current_node, current_node->next);
}

if( available_merges & MERGE_LEFT ) {
merge_memory_nodes(current_node->prev, current_node);
}
return;

}
current_node = current_node->next;
}
Expand All @@ -163,7 +163,7 @@ uint8_t get_kheap_size(KHeapMemoryNode *heap_start) {
uint8_t size = 0;
while( cur_node != NULL ) {
size++;
cur_node = cur_node->next;
cur_node = cur_node->next;
}
return size;
}
Expand Down Expand Up @@ -203,13 +203,13 @@ void merge_memory_nodes(KHeapMemoryNode *left_node, KHeapMemoryNode *right_node)
left_node->size = left_node->size + right_node->size + sizeof(KHeapMemoryNode);
//2. left_node next item will point to the next item of the right node (since the right node is going to disappear)
left_node->next = right_node->next;
//3. Unless we reached the last item, we should also make sure that the element after the right node, will be linked
//3. Unless we reached the last item, we should also make sure that the element after the right node, will be linked
// to the left node (via the prev field)
if(right_node->next != NULL){
KHeapMemoryNode *next_node = right_node->next;
next_node->prev = left_node;
}
}
}
}


Expand All @@ -224,13 +224,13 @@ KHeapMemoryNode* create_kheap_node( KHeapMemoryNode *current_node, size_t size )
new_node->size = current_node->size - (size + header_size);
new_node->prev = current_node;
new_node->next = current_node->next;

if( current_node->next != NULL) {
current_node->next->prev = new_node;
}

current_node->next = new_node;

if( current_node == kernel_heap_end) {
kernel_heap_end = new_node;
}
Expand Down
Loading

0 comments on commit dee5397

Please sign in to comment.