From 88b524543119a18297240a563cb8c08ff440dd53 Mon Sep 17 00:00:00 2001 From: the lemons Date: Sun, 4 Apr 2021 02:53:05 -0500 Subject: memory bookkeeping --- kernel.bin | Bin 87888 -> 89720 bytes os.iso | Bin 19890176 -> 19892224 bytes src/main.c | 7 ++++--- src/memory.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/memory.h | 35 +++++++++++++++++++++++++++++++++++ src/redacted.h | 11 +++++++++++ 6 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 src/memory.c create mode 100644 src/memory.h create mode 100644 src/redacted.h diff --git a/kernel.bin b/kernel.bin index a3d698f..1043589 100755 Binary files a/kernel.bin and b/kernel.bin differ diff --git a/os.iso b/os.iso index 9c9759f..a095b2f 100644 Binary files a/os.iso and b/os.iso differ diff --git a/src/main.c b/src/main.c index 0022a4c..a17db9a 100644 --- a/src/main.c +++ b/src/main.c @@ -3,8 +3,8 @@ #include "vga.h" #include "interrupt.h" -#include "string.h" #include "multiboot.h" +#include "memory.h" #include "printf.h" extern void *gdt; @@ -29,11 +29,12 @@ const char *pope = "by means of its diplomatic relations and political accords with many \n" "independent states.\n"; -void kernel_main(multiboot_info_t *mbd) { +void kernel_main(multiboot_info_t *mb) { vsetcolor(vga_color(vga_black, vga_white)); vclear(); interrupt_init(); start_interrupts(); + init_memory(mb); vprint(" _ _ \n"); vprint(" ___(_) |_ _ __ ___ _ __ ___ ___ ___ \n"); @@ -43,5 +44,5 @@ void kernel_main(multiboot_info_t *mbd) { vprint(pope); - while (1) asm("hlt"); + while (1) asm volatile("hlt"); } diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..970ba29 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,42 @@ + +#include +#include +#include +#include "memory.h" +#include "multiboot.h" + +struct memory_area { + uint32_t len; + struct memory_area *next; +}; + +struct memory_area *first = NULL; + +void init_memory(multiboot_info_t *mb) { + struct memory_area *prev = NULL; + for ( + multiboot_memory_map_t *e = (multiboot_memory_map_t *) mb->mmap_addr; + (unsigned int) e < mb->mmap_addr + mb->mmap_length; + e += 1 + ) { + if (e->type != MULTIBOOT_MEMORY_AVAILABLE) continue; + // memory below 1MB is not safe to use. + if (e->addr < 1000000) continue; + // memory above UINT_MAX is unusable. + if (e->addr > UINT_MAX) continue; + + uint32_t length; + // if the memory reaches outside of UINT_MAX, truncate length value + if (e->addr + e->len > UINT_MAX) + length = UINT_MAX - e->addr; + else length = e->len; + + struct memory_area *cur = (struct memory_area *) e->addr; + cur->len = length; + + if (prev != NULL) + prev->next = (struct memory_area *) e->addr; + + prev = cur; + } +} diff --git a/src/memory.h b/src/memory.h new file mode 100644 index 0000000..3c38af7 --- /dev/null +++ b/src/memory.h @@ -0,0 +1,35 @@ +#ifndef __MEMORY_H +#define __MEMORY_H + +#include +#include "redacted.h" +#include "multiboot.h" + +enum cell_type { + FREE_CELL, + CONS_CELL, + INT_CELL, + CHAR_CELL, + FLOAT_CELL, + ARRAY_CELL, + ARRAY_MEMBER, + APIOFORM +}; + +struct cell { + uint8_t type; + union car { + struct cell *free; + struct cell *cons; + struct {int type : 8; int size : 24;} ARRAY_CELL; + uint32_t int_; + uint32_t char_; + float float_; + REDACTED_t apioform; + }; + struct cell *cdr; +}; + +void init_memory(multiboot_info_t *mb); + +#endif diff --git a/src/redacted.h b/src/redacted.h new file mode 100644 index 0000000..5b5a667 --- /dev/null +++ b/src/redacted.h @@ -0,0 +1,11 @@ +#ifndef __REDACTED_H +#define __REDACTED_H + +typedef struct { + unsigned int type : 8; + unsigned int apiocity : 4; + int communism_value : 4; + int rotational_velocity : 16; +} REDACTED_t; + +#endif -- cgit v1.2.3