feature: memory limit with multiboot (wip, still crashing)

This commit is contained in:
0x35c 2024-11-07 13:45:29 +01:00
parent a72c3ca195
commit 5fccbf3708
7 changed files with 304 additions and 28 deletions

View File

@ -23,6 +23,7 @@ extern uint32_t _kernel_end;
extern uint32_t boot_page_directory; extern uint32_t boot_page_directory;
extern uint32_t *page_directory; extern uint32_t *page_directory;
extern uint32_t page_table_default[1024]; extern uint32_t page_table_default[1024];
extern uint32_t mem_size;
uint32_t *virt_to_phys(uint32_t *virt_addr); uint32_t *virt_to_phys(uint32_t *virt_addr);
void init_memory(void); void init_memory(void);

262
headers/multiboot.h Normal file
View File

@ -0,0 +1,262 @@
/* multiboot.h - Multiboot header file. */
/* Copyright (C) 1999,2003,2007,2008,2009,2010 Free Software Foundation, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY
* DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
/* How many bytes from the start of the file we search for the header. */
#define MULTIBOOT_SEARCH 8192
#define MULTIBOOT_HEADER_ALIGN 4
/* The magic field should contain this. */
#define MULTIBOOT_HEADER_MAGIC 0x1BADB002
/* This should be in %eax. */
#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002
/* Alignment of multiboot modules. */
#define MULTIBOOT_MOD_ALIGN 0x00001000
/* Alignment of the multiboot info structure. */
#define MULTIBOOT_INFO_ALIGN 0x00000004
/* Flags set in the flags member of the multiboot header. */
/* Align all boot modules on i386 page (4KB) boundaries. */
#define MULTIBOOT_PAGE_ALIGN 0x00000001
/* Must pass memory information to OS. */
#define MULTIBOOT_MEMORY_INFO 0x00000002
/* Must pass video information to OS. */
#define MULTIBOOT_VIDEO_MODE 0x00000004
/* This flag indicates the use of the address fields in the header. */
#define MULTIBOOT_AOUT_KLUDGE 0x00010000
/* Flags to be set in the flags member of the multiboot info structure. */
/* is there basic lower/upper memory information? */
#define MULTIBOOT_INFO_MEMORY 0x00000001
/* is there a boot device set? */
#define MULTIBOOT_INFO_BOOTDEV 0x00000002
/* is the command-line defined? */
#define MULTIBOOT_INFO_CMDLINE 0x00000004
/* are there modules to do something with? */
#define MULTIBOOT_INFO_MODS 0x00000008
/* These next two are mutually exclusive */
/* is there a symbol table loaded? */
#define MULTIBOOT_INFO_AOUT_SYMS 0x00000010
/* is there an ELF section header table? */
#define MULTIBOOT_INFO_ELF_SHDR 0X00000020
/* is there a full memory map? */
#define MULTIBOOT_INFO_MEM_MAP 0x00000040
/* Is there drive info? */
#define MULTIBOOT_INFO_DRIVE_INFO 0x00000080
/* Is there a config table? */
#define MULTIBOOT_INFO_CONFIG_TABLE 0x00000100
/* Is there a boot loader name? */
#define MULTIBOOT_INFO_BOOT_LOADER_NAME 0x00000200
/* Is there a APM table? */
#define MULTIBOOT_INFO_APM_TABLE 0x00000400
/* Is there video information? */
#define MULTIBOOT_INFO_VBE_INFO 0x00000800
#define MULTIBOOT_INFO_FRAMEBUFFER_INFO 0x00001000
#ifndef ASM_FILE
typedef unsigned char multiboot_uint8_t;
typedef unsigned short multiboot_uint16_t;
typedef unsigned int multiboot_uint32_t;
typedef unsigned long long multiboot_uint64_t;
struct multiboot_header {
/* Must be MULTIBOOT_MAGIC - see above. */
multiboot_uint32_t magic;
/* Feature flags. */
multiboot_uint32_t flags;
/* The above fields plus this one must equal 0 mod 2^32. */
multiboot_uint32_t checksum;
/* These are only valid if MULTIBOOT_AOUT_KLUDGE is set. */
multiboot_uint32_t header_addr;
multiboot_uint32_t load_addr;
multiboot_uint32_t load_end_addr;
multiboot_uint32_t bss_end_addr;
multiboot_uint32_t entry_addr;
/* These are only valid if MULTIBOOT_VIDEO_MODE is set. */
multiboot_uint32_t mode_type;
multiboot_uint32_t width;
multiboot_uint32_t height;
multiboot_uint32_t depth;
};
/* The symbol table for a.out. */
struct multiboot_aout_symbol_table {
multiboot_uint32_t tabsize;
multiboot_uint32_t strsize;
multiboot_uint32_t addr;
multiboot_uint32_t reserved;
};
typedef struct multiboot_aout_symbol_table multiboot_aout_symbol_table_t;
/* The section header table for ELF. */
struct multiboot_elf_section_header_table {
multiboot_uint32_t num;
multiboot_uint32_t size;
multiboot_uint32_t addr;
multiboot_uint32_t shndx;
};
typedef struct multiboot_elf_section_header_table
multiboot_elf_section_header_table_t;
struct multiboot_info {
/* Multiboot info version number */
multiboot_uint32_t flags;
/* Available memory from BIOS */
multiboot_uint32_t mem_lower;
multiboot_uint32_t mem_upper;
/* "root" partition */
multiboot_uint32_t boot_device;
/* Kernel command line */
multiboot_uint32_t cmdline;
/* Boot-Module list */
multiboot_uint32_t mods_count;
multiboot_uint32_t mods_addr;
union {
multiboot_aout_symbol_table_t aout_sym;
multiboot_elf_section_header_table_t elf_sec;
} u;
/* Memory Mapping buffer */
multiboot_uint32_t mmap_length;
multiboot_uint32_t mmap_addr;
/* Drive Info buffer */
multiboot_uint32_t drives_length;
multiboot_uint32_t drives_addr;
/* ROM configuration table */
multiboot_uint32_t config_table;
/* Boot Loader Name */
multiboot_uint32_t boot_loader_name;
/* APM table */
multiboot_uint32_t apm_table;
/* Video */
multiboot_uint32_t vbe_control_info;
multiboot_uint32_t vbe_mode_info;
multiboot_uint16_t vbe_mode;
multiboot_uint16_t vbe_interface_seg;
multiboot_uint16_t vbe_interface_off;
multiboot_uint16_t vbe_interface_len;
multiboot_uint64_t framebuffer_addr;
multiboot_uint32_t framebuffer_pitch;
multiboot_uint32_t framebuffer_width;
multiboot_uint32_t framebuffer_height;
multiboot_uint8_t framebuffer_bpp;
#define MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED 0
#define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1
#define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2
multiboot_uint8_t framebuffer_type;
union {
struct {
multiboot_uint32_t framebuffer_palette_addr;
multiboot_uint16_t framebuffer_palette_num_colors;
};
struct {
multiboot_uint8_t framebuffer_red_field_position;
multiboot_uint8_t framebuffer_red_mask_size;
multiboot_uint8_t framebuffer_green_field_position;
multiboot_uint8_t framebuffer_green_mask_size;
multiboot_uint8_t framebuffer_blue_field_position;
multiboot_uint8_t framebuffer_blue_mask_size;
};
};
};
typedef struct multiboot_info multiboot_info_t;
struct multiboot_color {
multiboot_uint8_t red;
multiboot_uint8_t green;
multiboot_uint8_t blue;
};
struct multiboot_mmap_entry {
multiboot_uint32_t size;
multiboot_uint64_t addr;
multiboot_uint64_t len;
#define MULTIBOOT_MEMORY_AVAILABLE 1
#define MULTIBOOT_MEMORY_RESERVED 2
#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3
#define MULTIBOOT_MEMORY_NVS 4
#define MULTIBOOT_MEMORY_BADRAM 5
multiboot_uint32_t type;
} __attribute__((packed));
typedef struct multiboot_mmap_entry multiboot_memory_map_t;
struct multiboot_mod_list {
/* the memory used goes from bytes mod_start to mod_end-1 inclusive
*/
multiboot_uint32_t mod_start;
multiboot_uint32_t mod_end;
/* Module command line */
multiboot_uint32_t cmdline;
/* padding to take it to 16 bytes (must be zero) */
multiboot_uint32_t pad;
};
typedef struct multiboot_mod_list multiboot_module_t;
/* APM BIOS info. */
struct multiboot_apm_info {
multiboot_uint16_t version;
multiboot_uint16_t cseg;
multiboot_uint32_t offset;
multiboot_uint16_t cseg_16;
multiboot_uint16_t dseg;
multiboot_uint16_t flags;
multiboot_uint16_t cseg_len;
multiboot_uint16_t cseg_16_len;
multiboot_uint16_t dseg_len;
};
#endif /* ! ASM_FILE */

View File

@ -113,6 +113,9 @@ _start:
# Set up the stack. # Set up the stack.
mov $stack_top, %esp mov $stack_top, %esp
push %eax
push %ebx
# Enter the high-level kernel. # Enter the high-level kernel.
call kernel_main call kernel_main

View File

@ -5,6 +5,7 @@
#include "idt.h" #include "idt.h"
#include "kprintf.h" #include "kprintf.h"
#include "memory.h" #include "memory.h"
#include "multiboot.h"
#include "power.h" #include "power.h"
#include "shell.h" #include "shell.h"
#include "string.h" #include "string.h"
@ -25,40 +26,48 @@
#error "This tutorial needs to be compiled with a ix86-elf compiler" #error "This tutorial needs to be compiled with a ix86-elf compiler"
#endif #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 :( */ /* 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(); terminal_initialize();
init_gdt(); init_gdt();
init_idt(); init_idt();
init_memory(); init_memory();
load_drivers(); load_drivers();
set_mem_size(mbd, magic);
kprintf(KERN_ALERT kprintf(KERN_ALERT
"I see no way to confuse an array of 256 seg:off pairs with a " "I see no way to confuse an array of 256 seg:off pairs with a "
"complex 8*unknown quantity -byte descriptor table. -- Troy " "complex 8*unknown quantity -byte descriptor table. -- Troy "
"Martin 03:50, 22 March 2009 (UTC)\n"); "Martin 03:50, 22 March 2009 (UTC)\n");
for (uint16_t i = 0;; i++) { /* vmalloc(10); */
void *ptr = alloc_pages(4096);
PRINT_PTR(ptr);
memset(ptr, ~0, 4096);
free_pages(ptr, 4096);
}
/* int i = 0; */
/* while (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(); shell_init();
} }

View File

@ -8,6 +8,7 @@
#include "utils.h" #include "utils.h"
#define MAX_FRAMES (HEAP_END / PAGE_SIZE) #define MAX_FRAMES (HEAP_END / PAGE_SIZE)
#define NB_FRAMES (mem_size / PAGE_SIZE)
enum { enum {
FREE, FREE,
@ -19,7 +20,7 @@ static uint8_t frame_table[MAX_FRAMES];
static int32_t find_next_block(size_t nb_frames) static int32_t find_next_block(size_t nb_frames)
{ {
for (uint32_t i = CEIL(HEAP_START, PAGE_SIZE); for (uint32_t i = CEIL(HEAP_START, PAGE_SIZE);
i + nb_frames < MAX_FRAMES; i++) { i + nb_frames < NB_FRAMES; i++) {
uint32_t j; uint32_t j;
for (j = 0; frame_table[i + j] == FREE && j < nb_frames; 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; return NULL;
} }
for (size_t j = 0; j < nb_frames; j++) { for (size_t j = 0; j < nb_frames; j++) {
assert(j + i < MAX_FRAMES); assert(j + i < NB_FRAMES);
frame_table[j + i] = USED; frame_table[j + i] = USED;
} }
return (void *)(i * PAGE_SIZE); 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 nb_frames = CEIL(size, PAGE_SIZE);
const uint32_t start = (uint32_t)frame_ptr / 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"); kprintf(KERN_WARNING "Address out of range\n");
return -1; return -1;
} else if ((uint32_t)frame_ptr % PAGE_SIZE) { } else if ((uint32_t)frame_ptr % PAGE_SIZE) {
kprintf(KERN_WARNING "Invalid address\n"); kprintf(KERN_WARNING "Invalid address\n");
return -1; return -1;
} else if (start + nb_frames > MAX_FRAMES) { } else if (start + nb_frames > NB_FRAMES) {
kprintf(KERN_WARNING "Invalid number of frames\n"); kprintf(KERN_WARNING "Invalid number of frames\n");
return -1; return -1;
} }

View File

@ -6,6 +6,7 @@
uint32_t *page_directory = &boot_page_directory; uint32_t *page_directory = &boot_page_directory;
uint32_t page_table_default[1024] __attribute__((aligned(4096))); uint32_t page_table_default[1024] __attribute__((aligned(4096)));
uint32_t mem_size;
void init_memory(void) void init_memory(void)
{ {

View File

@ -46,5 +46,4 @@ void merdella_cmd(char *arg)
{ {
(void)arg; (void)arg;
kprintf(POOP); kprintf(POOP);
int i = 1 / 0;
} }