feature: memory limit with multiboot (wip, still crashing)
This commit is contained in:
@ -113,6 +113,9 @@ _start:
|
||||
# Set up the stack.
|
||||
mov $stack_top, %esp
|
||||
|
||||
push %eax
|
||||
push %ebx
|
||||
|
||||
# Enter the high-level kernel.
|
||||
call kernel_main
|
||||
|
||||
|
55
src/kernel.c
55
src/kernel.c
@ -5,6 +5,7 @@
|
||||
#include "idt.h"
|
||||
#include "kprintf.h"
|
||||
#include "memory.h"
|
||||
#include "multiboot.h"
|
||||
#include "power.h"
|
||||
#include "shell.h"
|
||||
#include "string.h"
|
||||
@ -25,40 +26,48 @@
|
||||
#error "This tutorial needs to be compiled with a ix86-elf compiler"
|
||||
#endif
|
||||
|
||||
void kernel_main(void)
|
||||
/* Par contre je me dois d'être honnête avec toi, j'ai passé un très bon moment
|
||||
* hier soir c'était vraiment posé et tout mais j'ai pas trop ressenti de
|
||||
* feeling qui me donnerait envie de continuer dans une relation "couple" sur le
|
||||
* long terme. Je sais pas trop comment le dire sans que ça te blesse, c'est pas
|
||||
* personnel ou quoi c'est cool quand on parle et tout mais juste je ne pense
|
||||
* pas pouvoir faire marcher une relation comme celle là :( */ /* Voilà je voulais juste te le dire pour pas que tu te mettes quelque chose en
|
||||
* tête qui n'arrivera pas, je trouve ça plus correct */
|
||||
|
||||
static void set_mem_size(multiboot_info_t *mbd, uint32_t magic)
|
||||
{
|
||||
/* Make sure the magic number matches for memory mapping */
|
||||
if (magic != MULTIBOOT_BOOTLOADER_MAGIC)
|
||||
kpanic("invalid magic number!");
|
||||
|
||||
/* Check bit 6 to see if we have a valid memory map */
|
||||
if (!(mbd->flags >> 6 & 0x1))
|
||||
kpanic("invalid memory map given by GRUB bootloader");
|
||||
|
||||
/* Loop through the memory map and display the values */
|
||||
for (uint32_t i = 0; i < mbd->mmap_length;
|
||||
i += sizeof(multiboot_memory_map_t)) {
|
||||
multiboot_memory_map_t *mmmt =
|
||||
(multiboot_memory_map_t *)(mbd->mmap_addr + i);
|
||||
if (mmmt->type == MULTIBOOT_MEMORY_AVAILABLE)
|
||||
mem_size += mmmt->len;
|
||||
}
|
||||
}
|
||||
|
||||
void kernel_main(multiboot_info_t *mbd, uint32_t magic)
|
||||
{
|
||||
terminal_initialize();
|
||||
init_gdt();
|
||||
init_idt();
|
||||
init_memory();
|
||||
load_drivers();
|
||||
set_mem_size(mbd, magic);
|
||||
kprintf(KERN_ALERT
|
||||
"I see no way to confuse an array of 256 seg:off pairs with a "
|
||||
"complex 8*unknown quantity -byte descriptor table. -- Troy "
|
||||
"Martin 03:50, 22 March 2009 (UTC)\n");
|
||||
for (uint16_t i = 0;; i++) {
|
||||
void *ptr = alloc_pages(4096);
|
||||
PRINT_PTR(ptr);
|
||||
memset(ptr, ~0, 4096);
|
||||
free_pages(ptr, 4096);
|
||||
}
|
||||
/* int i = 0; */
|
||||
/* vmalloc(10); */
|
||||
/* while (vmalloc(10)) */
|
||||
/* ; */
|
||||
/* if (i++ > 70000) */
|
||||
/* kprintf("%d\n", i); */
|
||||
/* void *tab[1024]; */
|
||||
/* for (int i = 0; i < 1023; i++) { */
|
||||
/* tab[i] = alloc_pages(1, NULL); */
|
||||
/* PRINT_INT(i); */
|
||||
/* PRINT_PTR(tab[i]); */
|
||||
/* if (!tab[i]) */
|
||||
/* break; */
|
||||
/* memset(tab[i], i, 4096); */
|
||||
/* } */
|
||||
/* PRINT_UINT(((uint8_t *)tab[0])[0]); */
|
||||
/* for (int i = 0; i < 10; i++) */
|
||||
/* PRINT_UINT(((uint8_t *)tab[i])[0]); */
|
||||
/* PRINT_PTR(tab[i]); */
|
||||
shell_init();
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include "utils.h"
|
||||
|
||||
#define MAX_FRAMES (HEAP_END / PAGE_SIZE)
|
||||
#define NB_FRAMES (mem_size / PAGE_SIZE)
|
||||
|
||||
enum {
|
||||
FREE,
|
||||
@ -19,7 +20,7 @@ static uint8_t frame_table[MAX_FRAMES];
|
||||
static int32_t find_next_block(size_t nb_frames)
|
||||
{
|
||||
for (uint32_t i = CEIL(HEAP_START, PAGE_SIZE);
|
||||
i + nb_frames < MAX_FRAMES; i++) {
|
||||
i + nb_frames < NB_FRAMES; i++) {
|
||||
uint32_t j;
|
||||
for (j = 0; frame_table[i + j] == FREE && j < nb_frames; j++)
|
||||
;
|
||||
@ -39,7 +40,7 @@ void *alloc_frames(size_t size)
|
||||
return NULL;
|
||||
}
|
||||
for (size_t j = 0; j < nb_frames; j++) {
|
||||
assert(j + i < MAX_FRAMES);
|
||||
assert(j + i < NB_FRAMES);
|
||||
frame_table[j + i] = USED;
|
||||
}
|
||||
return (void *)(i * PAGE_SIZE);
|
||||
@ -50,13 +51,13 @@ int free_frames(void *frame_ptr, size_t size)
|
||||
const uint32_t nb_frames = CEIL(size, PAGE_SIZE);
|
||||
const uint32_t start = (uint32_t)frame_ptr / PAGE_SIZE;
|
||||
|
||||
if (start > MAX_FRAMES) {
|
||||
if (start > NB_FRAMES) {
|
||||
kprintf(KERN_WARNING "Address out of range\n");
|
||||
return -1;
|
||||
} else if ((uint32_t)frame_ptr % PAGE_SIZE) {
|
||||
kprintf(KERN_WARNING "Invalid address\n");
|
||||
return -1;
|
||||
} else if (start + nb_frames > MAX_FRAMES) {
|
||||
} else if (start + nb_frames > NB_FRAMES) {
|
||||
kprintf(KERN_WARNING "Invalid number of frames\n");
|
||||
return -1;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
uint32_t *page_directory = &boot_page_directory;
|
||||
uint32_t page_table_default[1024] __attribute__((aligned(4096)));
|
||||
uint32_t mem_size;
|
||||
|
||||
void init_memory(void)
|
||||
{
|
||||
|
@ -46,5 +46,4 @@ void merdella_cmd(char *arg)
|
||||
{
|
||||
(void)arg;
|
||||
kprintf(POOP);
|
||||
int i = 1 / 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user