From 58aa3807089bf0a414edf94de5eb555a4dd4f397 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Thu, 25 Nov 2021 12:36:35 +0100 Subject: [PATCH 01/38] Paging struc --- kernel/memory.c | 2 -- kernel/memory.h | 2 -- kernel/paging.c | 0 kernel/paging.h | 4 ++++ 4 files changed, 4 insertions(+), 4 deletions(-) delete mode 100644 kernel/memory.c delete mode 100644 kernel/memory.h create mode 100644 kernel/paging.c create mode 100644 kernel/paging.h diff --git a/kernel/memory.c b/kernel/memory.c deleted file mode 100644 index 8c5815e..0000000 --- a/kernel/memory.c +++ /dev/null @@ -1,2 +0,0 @@ -#include "memory.h" - diff --git a/kernel/memory.h b/kernel/memory.h deleted file mode 100644 index b2b140d..0000000 --- a/kernel/memory.h +++ /dev/null @@ -1,2 +0,0 @@ -char* malloc(unsigned int size); -void mfree(char* p); diff --git a/kernel/paging.c b/kernel/paging.c new file mode 100644 index 0000000..e69de29 diff --git a/kernel/paging.h b/kernel/paging.h new file mode 100644 index 0000000..c6d4bd3 --- /dev/null +++ b/kernel/paging.h @@ -0,0 +1,4 @@ +#define PAGE_SIZE 100 +#define PAGE_TABLE_SIZE 128 + +char* malloc(unsigned int pages); From 149f9945a9e47a05a6bd730d6e5ff6890ac34c76 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Thu, 25 Nov 2021 12:40:45 +0100 Subject: [PATCH 02/38] Page table & entry struc thing --- kernel/paging.c | 6 ++++++ kernel/paging.h | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/kernel/paging.c b/kernel/paging.c index e69de29..78318c2 100644 --- a/kernel/paging.c +++ b/kernel/paging.c @@ -0,0 +1,6 @@ +#include "paging.h" + +struct page_entry { + int index; + int start_addr; +} page_table[PAGE_TABLE_SIZE]; diff --git a/kernel/paging.h b/kernel/paging.h index c6d4bd3..e585f55 100644 --- a/kernel/paging.h +++ b/kernel/paging.h @@ -1,4 +1,4 @@ #define PAGE_SIZE 100 #define PAGE_TABLE_SIZE 128 -char* malloc(unsigned int pages); +struct page_entry; From 9d2aaf95c6cc34533ac7f07187be215660f78030 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Thu, 25 Nov 2021 12:47:19 +0100 Subject: [PATCH 03/38] Paging headers & comp errors --- kernel/paging.c | 8 +++++--- kernel/paging.h | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/kernel/paging.c b/kernel/paging.c index 78318c2..a4f1296 100644 --- a/kernel/paging.c +++ b/kernel/paging.c @@ -1,6 +1,8 @@ #include "paging.h" struct page_entry { - int index; - int start_addr; -} page_table[PAGE_TABLE_SIZE]; + unsigned int index; + unsigned int start_addr; +}; + +page_entry page_table[PAGE_TABLE_SIZE]; diff --git a/kernel/paging.h b/kernel/paging.h index e585f55..fa20b2e 100644 --- a/kernel/paging.h +++ b/kernel/paging.h @@ -2,3 +2,5 @@ #define PAGE_TABLE_SIZE 128 struct page_entry; +// page_entry page_table[PAGE_TABLE_SIZE]; + From 06d9d811d214cb5fe0c12a647262a805bc2c1b17 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 26 Nov 2021 08:29:17 +0100 Subject: [PATCH 04/38] Include fix --- kernel/kernel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/kernel.c b/kernel/kernel.c index ff1adce..07c8073 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -1,5 +1,5 @@ #include "kernel.h" -#include "memory.h" +#include "paging.h" #include "../drivers/vga.h" #include "../lib/str.h" #include "../lib/strf.h" From 7baf28a82d85fb76e2197bd770df8d19eeab1647 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 26 Nov 2021 08:38:39 +0100 Subject: [PATCH 05/38] MMU but in C --- kernel/paging.c | 10 ++++++++-- kernel/paging.h | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/kernel/paging.c b/kernel/paging.c index a4f1296..97faaab 100644 --- a/kernel/paging.c +++ b/kernel/paging.c @@ -1,8 +1,14 @@ #include "paging.h" +// Page Entry struct struct page_entry { unsigned int index; unsigned int start_addr; -}; +} page_table[PAGE_TABLE_SIZE]; -page_entry page_table[PAGE_TABLE_SIZE]; +int get_phys_addr(int virt_addr) { + int index = virt_addr / PAGE_SIZE; // page index for the virtual address + int offset = virt_addr % PAGE_SIZE; // actual physical offset for the address + + return page_table[index].start_addr + offset; +} diff --git a/kernel/paging.h b/kernel/paging.h index fa20b2e..9fb6bc2 100644 --- a/kernel/paging.h +++ b/kernel/paging.h @@ -2,5 +2,6 @@ #define PAGE_TABLE_SIZE 128 struct page_entry; -// page_entry page_table[PAGE_TABLE_SIZE]; +char** heap_alloc(unsigned int size); // Process heap allocation +int get_phys_addr(int virt_addr); From 5a239f5760671fd4d3943ac52475915a8238eb49 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 26 Nov 2021 09:23:29 +0100 Subject: [PATCH 06/38] Comp errors --- Makefile | 2 +- drivers/vga.c | 4 ++-- kernel/enable_paging.asm | 14 ++++++++++++++ kernel/kernel.c | 15 ++++++--------- kernel/paging.c | 10 ++++++++++ kernel/paging.h | 2 ++ 6 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 kernel/enable_paging.asm diff --git a/Makefile b/Makefile index 5cbd34e..f5935b7 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ eos_grub.iso : kernel.bin grub/grub.cfg eos.iso: bootloader/bootloader.bin kernel.bin cat $^ > eos.iso -kernel.bin: kernel/kernel_entry.o $(OBJ) +kernel.bin: kernel/kernel_entry.o kernel/enable_paging.o $(OBJ) gcc -o $@ $^ -Wl,--oformat=binary -ffreestanding -nostdlib -shared -Ttext 0x1000 -m32 diff --git a/drivers/vga.c b/drivers/vga.c index 45096a0..7633266 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -13,9 +13,9 @@ void vga_init() { // Clear screen // clear_row(0); - // clear_screen(); + clear_screen(); - set_cursor_pos(0, 11); + set_cursor_pos(0, 0); } /* diff --git a/kernel/enable_paging.asm b/kernel/enable_paging.asm new file mode 100644 index 0000000..97bae70 --- /dev/null +++ b/kernel/enable_paging.asm @@ -0,0 +1,14 @@ +[bits 32] ; 32-bit mode + +PAGING_ENABLE equ 0x80000001 +PAGE_DIRECTORY equ 0x8000 ; TODO: change me to something good + +enable_paging_registers: + mov eax, PAGE_DIRECTORY ; Move the address of the page register (page directory) into eax + ; (Using eax as a middle-man register) + mov cr3, eax ; Put the address into the cr3 register (required by the MMU) + + mov eax, cr0 ; eax as a middle-man register (again) + or eax, PAGING_ENABLE ; perform the OR operation on eax (ex: 0b01 or 0b10 = 0b11) + ; This is needed to enable paging (set the flag as "enabled") + mov cr0, eax ; Move it into cr0 to finally enable paging diff --git a/kernel/kernel.c b/kernel/kernel.c index 07c8073..70a0a72 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -4,19 +4,16 @@ #include "../lib/str.h" #include "../lib/strf.h" -void display_status(char* status_text, unsigned int bg_color) { - clear_row(0); - set_cursor_pos(0, 0); - - print(status_text, bg_color | STATUS_TEXT_COLOR); -} - void init() { - display_status("Kernel loaded", 0x70); - vga_init(); // Initialize the screen first // i.e. clear the screen et cetera. + println("Kernel loaded", DEFAULT_COLOR); + + enable_paging(); + + + println(""); char* title = "eOS Version 0.2 2021"; println(title, DEFAULT_COLOR); diff --git a/kernel/paging.c b/kernel/paging.c index 97faaab..7b5dc52 100644 --- a/kernel/paging.c +++ b/kernel/paging.c @@ -1,4 +1,14 @@ #include "paging.h" +#include "../drivers/vga.h" + +void enable_paging() { + println("Enabling paging...", DEFAULT_COLOR); + + extern int enable_paging_registers(); // Call the assembly SR + enable_paging_registers(); + + return; +} // Page Entry struct struct page_entry { diff --git a/kernel/paging.h b/kernel/paging.h index 9fb6bc2..026f644 100644 --- a/kernel/paging.h +++ b/kernel/paging.h @@ -1,6 +1,8 @@ #define PAGE_SIZE 100 #define PAGE_TABLE_SIZE 128 +void enable_paging(); + struct page_entry; char** heap_alloc(unsigned int size); // Process heap allocation From 2d05fb136452d6be9948fbbdf59e6c99283b68af Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 26 Nov 2021 09:25:11 +0100 Subject: [PATCH 07/38] Better naming --- kernel/enable_paging.asm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/enable_paging.asm b/kernel/enable_paging.asm index 97bae70..cc835bf 100644 --- a/kernel/enable_paging.asm +++ b/kernel/enable_paging.asm @@ -1,10 +1,11 @@ [bits 32] ; 32-bit mode PAGING_ENABLE equ 0x80000001 -PAGE_DIRECTORY equ 0x8000 ; TODO: change me to something good +PAGE_DIRECTORY_ADDR equ 0x80000000 ; TODO: change me to something good enable_paging_registers: - mov eax, PAGE_DIRECTORY ; Move the address of the page register (page directory) into eax + mov eax, PAGE_DIRECTORY_ADDR ; Move the address of the + ; page register (page directory) into eax ; (Using eax as a middle-man register) mov cr3, eax ; Put the address into the cr3 register (required by the MMU) From c9acd8875f58bb8f58026d2b10bf67b733961045 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 26 Nov 2021 09:42:46 +0100 Subject: [PATCH 08/38] Enable paging stuff --- kernel/enable_paging.asm | 3 +++ kernel/kernel.c | 2 +- kernel/paging.c | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/kernel/enable_paging.asm b/kernel/enable_paging.asm index cc835bf..5412ab7 100644 --- a/kernel/enable_paging.asm +++ b/kernel/enable_paging.asm @@ -13,3 +13,6 @@ enable_paging_registers: or eax, PAGING_ENABLE ; perform the OR operation on eax (ex: 0b01 or 0b10 = 0b11) ; This is needed to enable paging (set the flag as "enabled") mov cr0, eax ; Move it into cr0 to finally enable paging + + +global enable_paging_registers ; make the SR "global" so that we can access it in the kernel etc diff --git a/kernel/kernel.c b/kernel/kernel.c index 70a0a72..14c61da 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -14,7 +14,7 @@ void init() { println(""); - char* title = "eOS Version 0.2 2021"; + char* title = "eOS Version 0.3 2021"; println(title, DEFAULT_COLOR); char* subtitle = "A x86 operating system, licenced under GPL-2.0"; diff --git a/kernel/paging.c b/kernel/paging.c index 7b5dc52..a51c366 100644 --- a/kernel/paging.c +++ b/kernel/paging.c @@ -4,7 +4,7 @@ void enable_paging() { println("Enabling paging...", DEFAULT_COLOR); - extern int enable_paging_registers(); // Call the assembly SR + extern void enable_paging_registers(); // Call the assembly SR enable_paging_registers(); return; From 3c891cad574bcb5172163f8de76d746095902258 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 26 Nov 2021 09:49:51 +0100 Subject: [PATCH 09/38] Removed unused header --- kernel/enable_paging.asm | 2 +- kernel/paging.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/enable_paging.asm b/kernel/enable_paging.asm index 5412ab7..878b33a 100644 --- a/kernel/enable_paging.asm +++ b/kernel/enable_paging.asm @@ -1,7 +1,7 @@ [bits 32] ; 32-bit mode PAGING_ENABLE equ 0x80000001 -PAGE_DIRECTORY_ADDR equ 0x80000000 ; TODO: change me to something good +PAGE_DIRECTORY_ADDR equ 0xf000000f ; TODO: change me to something good enable_paging_registers: mov eax, PAGE_DIRECTORY_ADDR ; Move the address of the diff --git a/kernel/paging.h b/kernel/paging.h index 026f644..ad4e4a8 100644 --- a/kernel/paging.h +++ b/kernel/paging.h @@ -5,5 +5,5 @@ void enable_paging(); struct page_entry; -char** heap_alloc(unsigned int size); // Process heap allocation +//char** heap_alloc(unsigned int size); // Process heap allocation int get_phys_addr(int virt_addr); From 9ffbcc8ad0a91ddb621cdcfd1b0f8e0b88a1e3ac Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 26 Nov 2021 10:00:08 +0100 Subject: [PATCH 10/38] Bug fix --- kernel/enable_paging.asm | 10 ++++++---- kernel/paging.c | 4 ++-- kernel/paging.h | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/kernel/enable_paging.asm b/kernel/enable_paging.asm index 878b33a..4a90dd2 100644 --- a/kernel/enable_paging.asm +++ b/kernel/enable_paging.asm @@ -1,8 +1,9 @@ [bits 32] ; 32-bit mode -PAGING_ENABLE equ 0x80000001 -PAGE_DIRECTORY_ADDR equ 0xf000000f ; TODO: change me to something good +PAGING_ENABLE_FLAG equ 0x80000001 +PAGE_DIRECTORY_ADDR equ 0xffffffff ; TODO: change me to something good +global enable_paging_registers ; make the SR "global" so that we can access it in the kernel etc enable_paging_registers: mov eax, PAGE_DIRECTORY_ADDR ; Move the address of the ; page register (page directory) into eax @@ -10,9 +11,10 @@ enable_paging_registers: mov cr3, eax ; Put the address into the cr3 register (required by the MMU) mov eax, cr0 ; eax as a middle-man register (again) - or eax, PAGING_ENABLE ; perform the OR operation on eax (ex: 0b01 or 0b10 = 0b11) + or eax, PAGING_ENABLE_FLAG ; perform the OR operation on eax (ex: 0b01 or 0b10 = 0b11) ; This is needed to enable paging (set the flag as "enabled") mov cr0, eax ; Move it into cr0 to finally enable paging + ret ; return to last location + -global enable_paging_registers ; make the SR "global" so that we can access it in the kernel etc diff --git a/kernel/paging.c b/kernel/paging.c index a51c366..9aa234b 100644 --- a/kernel/paging.c +++ b/kernel/paging.c @@ -4,8 +4,8 @@ void enable_paging() { println("Enabling paging...", DEFAULT_COLOR); - extern void enable_paging_registers(); // Call the assembly SR - enable_paging_registers(); + extern int enable_paging_registers(); // Call the assembly SR + enable_paging_registers(); // and enable paging return; } diff --git a/kernel/paging.h b/kernel/paging.h index ad4e4a8..fe16c13 100644 --- a/kernel/paging.h +++ b/kernel/paging.h @@ -5,5 +5,5 @@ void enable_paging(); struct page_entry; -//char** heap_alloc(unsigned int size); // Process heap allocation +// char** heap_alloc(unsigned int size); // Process heap allocation int get_phys_addr(int virt_addr); From f2d454d3d5328901083487d966ed237995255a14 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 26 Nov 2021 10:04:41 +0100 Subject: [PATCH 11/38] Implemented a nice kernel death. Nothing works --- kernel/enable_paging.asm | 3 +++ kernel/kernel.c | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/enable_paging.asm b/kernel/enable_paging.asm index 4a90dd2..ff66fc7 100644 --- a/kernel/enable_paging.asm +++ b/kernel/enable_paging.asm @@ -13,7 +13,10 @@ enable_paging_registers: mov eax, cr0 ; eax as a middle-man register (again) or eax, PAGING_ENABLE_FLAG ; perform the OR operation on eax (ex: 0b01 or 0b10 = 0b11) ; This is needed to enable paging (set the flag as "enabled") + + ; DANGER: past this op memory will get weird mov cr0, eax ; Move it into cr0 to finally enable paging + ; TODO: Make this work to prevent kernel "panic" (actually death) -> bootloop ret ; return to last location diff --git a/kernel/kernel.c b/kernel/kernel.c index 14c61da..66e2c3f 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -12,7 +12,6 @@ void init() { enable_paging(); - println(""); char* title = "eOS Version 0.3 2021"; println(title, DEFAULT_COLOR); From e2a5e5c33a0dbe9244e64839403221709e0d9d0d Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 26 Nov 2021 10:05:01 +0100 Subject: [PATCH 12/38] etc --- kernel/enable_paging.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/enable_paging.asm b/kernel/enable_paging.asm index ff66fc7..5a24d15 100644 --- a/kernel/enable_paging.asm +++ b/kernel/enable_paging.asm @@ -15,7 +15,7 @@ enable_paging_registers: ; This is needed to enable paging (set the flag as "enabled") ; DANGER: past this op memory will get weird - mov cr0, eax ; Move it into cr0 to finally enable paging + ; mov cr0, eax ; Move it into cr0 to finally enable paging ; TODO: Make this work to prevent kernel "panic" (actually death) -> bootloop ret ; return to last location From 73ece9d19e56ac2b53587702e84c903967baa352 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Sat, 27 Nov 2021 16:31:10 +0100 Subject: [PATCH 13/38] Non-distructive assembly stuff --- kernel/enable_paging.asm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/enable_paging.asm b/kernel/enable_paging.asm index 5a24d15..9a12923 100644 --- a/kernel/enable_paging.asm +++ b/kernel/enable_paging.asm @@ -5,6 +5,7 @@ PAGE_DIRECTORY_ADDR equ 0xffffffff ; TODO: change me to something good global enable_paging_registers ; make the SR "global" so that we can access it in the kernel etc enable_paging_registers: + push eax mov eax, PAGE_DIRECTORY_ADDR ; Move the address of the ; page register (page directory) into eax ; (Using eax as a middle-man register) @@ -14,10 +15,9 @@ enable_paging_registers: or eax, PAGING_ENABLE_FLAG ; perform the OR operation on eax (ex: 0b01 or 0b10 = 0b11) ; This is needed to enable paging (set the flag as "enabled") - ; DANGER: past this op memory will get weird - ; mov cr0, eax ; Move it into cr0 to finally enable paging - ; TODO: Make this work to prevent kernel "panic" (actually death) -> bootloop + mov cr0, eax ; Move it into cr0 to finally enable paging + pop eax ret ; return to last location From 6cc1994b3e49ba374c2e8ccc7d7929036efbd868 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Sat, 27 Nov 2021 16:57:55 +0100 Subject: [PATCH 14/38] Stuff --- kernel/paging.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/paging.c b/kernel/paging.c index 9aa234b..f221246 100644 --- a/kernel/paging.c +++ b/kernel/paging.c @@ -4,8 +4,8 @@ void enable_paging() { println("Enabling paging...", DEFAULT_COLOR); - extern int enable_paging_registers(); // Call the assembly SR - enable_paging_registers(); // and enable paging + // extern int enable_paging_registers(); // Call the assembly SR + // enable_paging_registers(); // and enable paging return; } From 92d5cea3628351d505db74684059face26b7184c Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Sat, 27 Nov 2021 17:30:24 +0100 Subject: [PATCH 15/38] Removed paging crap --- kernel/kernel.c | 2 +- kernel/paging.c | 22 +++++++++++----------- kernel/paging.h | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/kernel/kernel.c b/kernel/kernel.c index 66e2c3f..d6219b9 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -10,7 +10,7 @@ void init() { println("Kernel loaded", DEFAULT_COLOR); - enable_paging(); + // enable_paging(); println(""); char* title = "eOS Version 0.3 2021"; diff --git a/kernel/paging.c b/kernel/paging.c index f221246..3f020b8 100644 --- a/kernel/paging.c +++ b/kernel/paging.c @@ -11,14 +11,14 @@ void enable_paging() { } // Page Entry struct -struct page_entry { - unsigned int index; - unsigned int start_addr; -} page_table[PAGE_TABLE_SIZE]; - -int get_phys_addr(int virt_addr) { - int index = virt_addr / PAGE_SIZE; // page index for the virtual address - int offset = virt_addr % PAGE_SIZE; // actual physical offset for the address - - return page_table[index].start_addr + offset; -} +//struct page_entry { +// unsigned int index; +// unsigned int start_addr; +//} page_table[PAGE_TABLE_SIZE]; +// +//int get_phys_addr(int virt_addr) { +// int index = virt_addr / PAGE_SIZE; // page index for the virtual address +// int offset = virt_addr % PAGE_SIZE; // actual physical offset for the address +// +// return page_table[index].start_addr + offset; +//} diff --git a/kernel/paging.h b/kernel/paging.h index fe16c13..a1e88ff 100644 --- a/kernel/paging.h +++ b/kernel/paging.h @@ -3,7 +3,7 @@ void enable_paging(); -struct page_entry; +// struct page_entry; // char** heap_alloc(unsigned int size); // Process heap allocation -int get_phys_addr(int virt_addr); +// int get_phys_addr(int virt_addr); From 0529ecc2989e0e7988909a659f88e0723c1299b5 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 3 Dec 2021 09:08:06 +0100 Subject: [PATCH 16/38] Memory stuff --- kernel/kernel.c | 1 + kernel/memory.c | 14 ++++++++++++++ kernel/memory.h | 5 +++++ 3 files changed, 20 insertions(+) create mode 100644 kernel/memory.c create mode 100644 kernel/memory.h diff --git a/kernel/kernel.c b/kernel/kernel.c index d6219b9..b699ccc 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -1,4 +1,5 @@ #include "kernel.h" +#include "memory.h" #include "paging.h" #include "../drivers/vga.h" #include "../lib/str.h" diff --git a/kernel/memory.c b/kernel/memory.c new file mode 100644 index 0000000..bb577e4 --- /dev/null +++ b/kernel/memory.c @@ -0,0 +1,14 @@ +#include "memory.h" + +// https://wiki.osdev.org/Page_Frame_Allocation +// page = block + +// MAX_BLOCK_SIZE_IN_BITS/8 bytes +// i.e. : bit i of byte n define status of block 8n+i +// block = 8n+i + +int* alloc_cursor; // keep track of last location that was allocated (may improve speed) + +void init_pmm(unsigned int map_addr, unsigned int bsize) { + +} diff --git a/kernel/memory.h b/kernel/memory.h new file mode 100644 index 0000000..5d46f0b --- /dev/null +++ b/kernel/memory.h @@ -0,0 +1,5 @@ +#define BLOCK_SIZE 4 // 32 bits = 4 bytes +#define MEMSIZE_TO_BLOCKS(n) ((n*1024)/BLOCK_SIZE) + +void init_pmm(unsigned int map_addr, unsigned int bsize); // Initialize physical memory manager + From 95e2f274e3720391cad13db477d5ad4cec4c8fe3 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 3 Dec 2021 09:44:52 +0100 Subject: [PATCH 17/38] Bitmap thing --- Makefile | 2 +- kernel/memory.c | 14 ++++++++++++-- kernel/memory.h | 9 +++++++-- kernel/sr_memory.asm | 0 lib/types.h | 1 + 5 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 kernel/sr_memory.asm create mode 100644 lib/types.h diff --git a/Makefile b/Makefile index f5935b7..bece67e 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ eos_grub.iso : kernel.bin grub/grub.cfg eos.iso: bootloader/bootloader.bin kernel.bin cat $^ > eos.iso -kernel.bin: kernel/kernel_entry.o kernel/enable_paging.o $(OBJ) +kernel.bin: kernel/kernel_entry.o kernel/sr_memory.o kernel/enable_paging.o $(OBJ) gcc -o $@ $^ -Wl,--oformat=binary -ffreestanding -nostdlib -shared -Ttext 0x1000 -m32 diff --git a/kernel/memory.c b/kernel/memory.c index bb577e4..236e93e 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -7,8 +7,18 @@ // i.e. : bit i of byte n define status of block 8n+i // block = 8n+i -int* alloc_cursor; // keep track of last location that was allocated (may improve speed) +// in 32-bit mode, we have access to 2^32 blocks +// which is (2^32)*BLOCK_SIZE blocks +// and with a blocksize of 1024, we git around 4.3 trillion blocks +// which is more than enough -void init_pmm(unsigned int map_addr, unsigned int bsize) { +int bitmap = 0; +enum bitmap_flag { + FREE, + ALLOC +}; + +void mod_bitmap(uint block, uint bit, uint bflag) { + } diff --git a/kernel/memory.h b/kernel/memory.h index 5d46f0b..93cfa77 100644 --- a/kernel/memory.h +++ b/kernel/memory.h @@ -1,5 +1,10 @@ -#define BLOCK_SIZE 4 // 32 bits = 4 bytes +#include "../lib/types.h" + +#define BLOCK_SIZE 1024 // 1 KiB #define MEMSIZE_TO_BLOCKS(n) ((n*1024)/BLOCK_SIZE) -void init_pmm(unsigned int map_addr, unsigned int bsize); // Initialize physical memory manager +enum bitmap_flag; + +// void init_pmm(uint map_addr, uint bsize); // Initialize physical memory manager +void mod_bitmap(uint block, uint bit, uint flag); diff --git a/kernel/sr_memory.asm b/kernel/sr_memory.asm new file mode 100644 index 0000000..e69de29 diff --git a/lib/types.h b/lib/types.h new file mode 100644 index 0000000..9113551 --- /dev/null +++ b/lib/types.h @@ -0,0 +1 @@ +typedef unsigned int uint; From 5654680f7ef514d5f056223a8699c1073e5af709 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 3 Dec 2021 09:56:31 +0100 Subject: [PATCH 18/38] mod_bitmap --- kernel/memory.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/kernel/memory.c b/kernel/memory.c index 236e93e..17e1e44 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -11,14 +11,13 @@ // which is (2^32)*BLOCK_SIZE blocks // and with a blocksize of 1024, we git around 4.3 trillion blocks // which is more than enough - int bitmap = 0; -enum bitmap_flag { - FREE, - ALLOC -}; - void mod_bitmap(uint block, uint bit, uint bflag) { - + // create a bitmask that will be applied to the bitmap + int bitmask = 1 << bit; + + // apply the bitmask, resulting in the bit:n bit will bet + // set to bflag + bitmap = (((bitmap & ~bitmask)) | (bflag << bit)); } From 1c9a865f1ce8cbe8ab54fc0602ec9d252cc6c176 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 3 Dec 2021 10:14:35 +0100 Subject: [PATCH 19/38] Broken bitmap --- kernel/kernel.c | 5 +++++ kernel/memory.c | 20 ++++++++++++++++++-- kernel/memory.h | 8 +++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/kernel/kernel.c b/kernel/kernel.c index b699ccc..5bb32e4 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -25,6 +25,11 @@ void init() { println("0x1000", DEFAULT_COLOR); */ + block_alloc(1); + block_alloc(1); + block_alloc(2); + block_alloc(2); + char* strbuf = "Concat test: "; char* str2 = "Works!"; strbuf = strcat(strbuf, str2); diff --git a/kernel/memory.c b/kernel/memory.c index 17e1e44..43083b3 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -1,4 +1,5 @@ #include "memory.h" +#include "../drivers/vga.h" // https://wiki.osdev.org/Page_Frame_Allocation // page = block @@ -11,9 +12,11 @@ // which is (2^32)*BLOCK_SIZE blocks // and with a blocksize of 1024, we git around 4.3 trillion blocks // which is more than enough -int bitmap = 0; +int bitmap = 0; -void mod_bitmap(uint block, uint bit, uint bflag) { +#define CHECK_BITMAP(map, idx) ((map) & (1<<(idx))) + +void mod_bitmap(uint bit, uint bflag) { // create a bitmask that will be applied to the bitmap int bitmask = 1 << bit; @@ -21,3 +24,16 @@ void mod_bitmap(uint block, uint bit, uint bflag) { // set to bflag bitmap = (((bitmap & ~bitmask)) | (bflag << bit)); } + + +int block_alloc(uint blockidx) { + if( CHECK_BITMAP(bitmap, blockidx) == 1 ) { // check if block is free + println("Alloc!", DEFAULT_COLOR); + + mod_bitmap(blockidx, 0); + + } else { + println("ERROR! Attemped to allocate non-free block.", 0x0c); + return -1; + } +} diff --git a/kernel/memory.h b/kernel/memory.h index 93cfa77..84e31c2 100644 --- a/kernel/memory.h +++ b/kernel/memory.h @@ -1,10 +1,12 @@ #include "../lib/types.h" #define BLOCK_SIZE 1024 // 1 KiB +#define MAX_BLOCK_COUNT 32 // placeholder #define MEMSIZE_TO_BLOCKS(n) ((n*1024)/BLOCK_SIZE) -enum bitmap_flag; - // void init_pmm(uint map_addr, uint bsize); // Initialize physical memory manager -void mod_bitmap(uint block, uint bit, uint flag); +void mod_bitmap(uint bit, uint flag); + +int block_alloc(uint blockidx); +int block_free(uint blockidx); From b7b9c9e9759e2af2b238df13ad09d3157d198131 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 10 Dec 2021 08:18:31 +0100 Subject: [PATCH 20/38] Stuff --- kernel/memory.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/memory.c b/kernel/memory.c index 43083b3..3e3e5c8 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -31,6 +31,7 @@ int block_alloc(uint blockidx) { println("Alloc!", DEFAULT_COLOR); mod_bitmap(blockidx, 0); + return 0; // placeholder } else { println("ERROR! Attemped to allocate non-free block.", 0x0c); From 684785a49da8c4b4d5e172901a0bcb910c5798b7 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 10 Dec 2021 09:09:24 +0100 Subject: [PATCH 21/38] Debug & more stuff --- kernel/kernel.c | 13 +++++++++---- kernel/memory.c | 14 +++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/kernel/kernel.c b/kernel/kernel.c index 5bb32e4..46b224d 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -25,10 +25,15 @@ void init() { println("0x1000", DEFAULT_COLOR); */ - block_alloc(1); - block_alloc(1); - block_alloc(2); - block_alloc(2); + + // Memory allocation testing + println("THESE ALLOC SHOULD WORK:", 0xa0); + for(int i=0; i < 4; i++) { + block_alloc(i); + } + + println("(2) THIS ALLOC SHOULD FAIL:", 0xc0); + block_alloc(2); // this should fail char* strbuf = "Concat test: "; char* str2 = "Works!"; diff --git a/kernel/memory.c b/kernel/memory.c index 3e3e5c8..77bc1fd 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -12,29 +12,33 @@ // which is (2^32)*BLOCK_SIZE blocks // and with a blocksize of 1024, we git around 4.3 trillion blocks // which is more than enough -int bitmap = 0; #define CHECK_BITMAP(map, idx) ((map) & (1<<(idx))) +int bitmap = 0; void mod_bitmap(uint bit, uint bflag) { // create a bitmask that will be applied to the bitmap int bitmask = 1 << bit; - // apply the bitmask, resulting in the bit:n bit will bet + // apply the bitmask, resulting in the bit:n bit will set // set to bflag bitmap = (((bitmap & ~bitmask)) | (bflag << bit)); } int block_alloc(uint blockidx) { - if( CHECK_BITMAP(bitmap, blockidx) == 1 ) { // check if block is free + int block_bflag; + block_bflag = CHECK_BITMAP(bitmap, blockidx); + + if( block_bflag != 1 ) { // check if block is free println("Alloc!", DEFAULT_COLOR); + println(block_bflag, 0x8e); - mod_bitmap(blockidx, 0); + mod_bitmap(blockidx, 1); return 0; // placeholder - } else { println("ERROR! Attemped to allocate non-free block.", 0x0c); + println(block_bflag, 0x9c); return -1; } } From c1c9248e1ec9d5f27496d82c456d9e910108f8d6 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 10 Dec 2021 09:41:15 +0100 Subject: [PATCH 22/38] Bitmap bugs --- drivers/vga.c | 8 ++++++++ drivers/vga.h | 1 + kernel/memory.c | 5 +++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/drivers/vga.c b/drivers/vga.c index 7633266..889c3bc 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -2,6 +2,7 @@ #include "vga.h" #include "../kernel/io.h" #include "../lib/str.h" +#include "../lib/strf.h" static unsigned int cursor_row = 0; static unsigned int cursor_col = 0; @@ -69,6 +70,13 @@ void println(char* str, int attribute_byte) { cursor_row++; // Increment to next y-pos (newline) } +void printint(int i, int attribute_byte) { + char* strbuf; + + strbuf = int_to_str(i, strbuf); + println(strbuf, attribute_byte); +} + void printalign(char* str, int attribute_byte, enum align alignment) { unsigned int strlenbuf = strlen(str); diff --git a/drivers/vga.h b/drivers/vga.h index 82c5af4..f8f1d08 100644 --- a/drivers/vga.h +++ b/drivers/vga.h @@ -17,5 +17,6 @@ void clear_row(unsigned int row); void set_cursor_pos(); void print(); void println(); +void printint(int i, int attribute_byte); void printalign(char* str, int attribute_byte, enum align alignment); void vga_init(); diff --git a/kernel/memory.c b/kernel/memory.c index 77bc1fd..7beb9fe 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -15,7 +15,7 @@ #define CHECK_BITMAP(map, idx) ((map) & (1<<(idx))) -int bitmap = 0; +static int bitmap = 0; void mod_bitmap(uint bit, uint bflag) { // create a bitmask that will be applied to the bitmap int bitmask = 1 << bit; @@ -38,7 +38,8 @@ int block_alloc(uint blockidx) { return 0; // placeholder } else { println("ERROR! Attemped to allocate non-free block.", 0x0c); - println(block_bflag, 0x9c); + printint(block_bflag, 0x9c); + return -1; } } From c169f3ba86dadead02bcb05f89ad095935f60564 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 10 Dec 2021 09:44:40 +0100 Subject: [PATCH 23/38] BITMAP ALLOC WORKS :DDDD --- kernel/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/memory.c b/kernel/memory.c index 7beb9fe..bd52b49 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -30,7 +30,7 @@ int block_alloc(uint blockidx) { int block_bflag; block_bflag = CHECK_BITMAP(bitmap, blockidx); - if( block_bflag != 1 ) { // check if block is free + if( block_bflag == 0 ) { // check if block is free println("Alloc!", DEFAULT_COLOR); println(block_bflag, 0x8e); From 71590736270750bbb19a8152073ba5eb4385171d Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 10 Dec 2021 09:49:07 +0100 Subject: [PATCH 24/38] Alloc testing --- kernel/kernel.c | 4 ++++ kernel/memory.c | 11 ++++++++--- kernel/memory.h | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/kernel/kernel.c b/kernel/kernel.c index 46b224d..fb2ae98 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -35,6 +35,10 @@ void init() { println("(2) THIS ALLOC SHOULD FAIL:", 0xc0); block_alloc(2); // this should fail + println("(2) Freeing 2nd block, alloc after should succeed", DEFAULT_COLOR); + block_free(2); + block_alloc(2); + char* strbuf = "Concat test: "; char* str2 = "Works!"; strbuf = strcat(strbuf, str2); diff --git a/kernel/memory.c b/kernel/memory.c index bd52b49..752137e 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -15,6 +15,8 @@ #define CHECK_BITMAP(map, idx) ((map) & (1<<(idx))) +#define BM_FREE 0 + static int bitmap = 0; void mod_bitmap(uint bit, uint bflag) { // create a bitmask that will be applied to the bitmap @@ -30,16 +32,19 @@ int block_alloc(uint blockidx) { int block_bflag; block_bflag = CHECK_BITMAP(bitmap, blockidx); - if( block_bflag == 0 ) { // check if block is free + if( block_bflag == BM_FREE ) { // check if block is free println("Alloc!", DEFAULT_COLOR); - println(block_bflag, 0x8e); mod_bitmap(blockidx, 1); return 0; // placeholder } else { println("ERROR! Attemped to allocate non-free block.", 0x0c); - printint(block_bflag, 0x9c); return -1; } } + +void block_free(uint blockidx) { + println("Dealloc block...", DEFAULT_COLOR); + mod_bitmap(blockidx, BM_FREE); +} diff --git a/kernel/memory.h b/kernel/memory.h index 84e31c2..c4304f4 100644 --- a/kernel/memory.h +++ b/kernel/memory.h @@ -9,4 +9,4 @@ void mod_bitmap(uint bit, uint flag); int block_alloc(uint blockidx); -int block_free(uint blockidx); +void block_free(uint blockidx); From 9a564b727836d21191fd55c98463e05918c1e1e9 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 10 Dec 2021 10:05:18 +0100 Subject: [PATCH 25/38] Alloc stuff --- drivers/vga.c | 4 ++++ kernel/kernel.c | 2 +- kernel/memory.c | 15 +++++++++++++-- kernel/memory.h | 14 ++++++++++++-- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/drivers/vga.c b/drivers/vga.c index 889c3bc..066c3b5 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -1,6 +1,7 @@ // VGA Graphics Library #include "vga.h" #include "../kernel/io.h" +#include "../kernel/memory.h" #include "../lib/str.h" #include "../lib/strf.h" @@ -17,6 +18,9 @@ void vga_init() { clear_screen(); set_cursor_pos(0, 0); + + // TODO: allocate VGA physical memory + // pm_mem_alloc(VGA_ADDRESS, VGA_ADDRESS_MAX); } /* diff --git a/kernel/kernel.c b/kernel/kernel.c index fb2ae98..b979f15 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -36,7 +36,7 @@ void init() { block_alloc(2); // this should fail println("(2) Freeing 2nd block, alloc after should succeed", DEFAULT_COLOR); - block_free(2); + block_free(2); // after this, allocation of 2nd block should work block_alloc(2); char* strbuf = "Concat test: "; diff --git a/kernel/memory.c b/kernel/memory.c index 752137e..ad6744c 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -15,9 +15,11 @@ #define CHECK_BITMAP(map, idx) ((map) & (1<<(idx))) -#define BM_FREE 0 +#define BLOCK_TO_MEMP(idx) idx + static int bitmap = 0; +static uint last_block; void mod_bitmap(uint bit, uint bflag) { // create a bitmask that will be applied to the bitmap int bitmask = 1 << bit; @@ -28,7 +30,7 @@ void mod_bitmap(uint bit, uint bflag) { } -int block_alloc(uint blockidx) { +int* block_alloc(uint blockidx) { int block_bflag; block_bflag = CHECK_BITMAP(bitmap, blockidx); @@ -36,6 +38,8 @@ int block_alloc(uint blockidx) { println("Alloc!", DEFAULT_COLOR); mod_bitmap(blockidx, 1); + last_block = blockidx; + return 0; // placeholder } else { println("ERROR! Attemped to allocate non-free block.", 0x0c); @@ -47,4 +51,11 @@ int block_alloc(uint blockidx) { void block_free(uint blockidx) { println("Dealloc block...", DEFAULT_COLOR); mod_bitmap(blockidx, BM_FREE); + last_block = blockidx; +} + +/* +int* pm_malloc(uint block_count) { + } +*/ diff --git a/kernel/memory.h b/kernel/memory.h index c4304f4..156de0a 100644 --- a/kernel/memory.h +++ b/kernel/memory.h @@ -4,9 +4,19 @@ #define MAX_BLOCK_COUNT 32 // placeholder #define MEMSIZE_TO_BLOCKS(n) ((n*1024)/BLOCK_SIZE) +#define BM_FREE 0 + +#define PM_MEM_START 0x000000 // start of memory +#define PM_MEM_END 0xb7000 // end of memory TODO: fix me/change or something + // void init_pmm(uint map_addr, uint bsize); // Initialize physical memory manager void mod_bitmap(uint bit, uint flag); -int block_alloc(uint blockidx); -void block_free(uint blockidx); +int* block_alloc(uint blockidx); // allocate a block +void block_free(uint blockidx); // free a block + +void pm_alloc_range(uint start, uint end, bool force); // allocate a range of memory + +int* pm_malloc(uint block_count); // allocate some blocks +void pm_free(int* p); // free a var (if allocated with pm_malloc) From 815a6090090a719151ec35c2d2d251c7f1dd673a Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 10 Dec 2021 13:05:34 +0100 Subject: [PATCH 26/38] Comment --- kernel/memory.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/memory.h b/kernel/memory.h index 156de0a..431b8ca 100644 --- a/kernel/memory.h +++ b/kernel/memory.h @@ -16,7 +16,7 @@ void mod_bitmap(uint bit, uint flag); int* block_alloc(uint blockidx); // allocate a block void block_free(uint blockidx); // free a block -void pm_alloc_range(uint start, uint end, bool force); // allocate a range of memory +//void pm_alloc_range(uint start, uint end, bool force); // allocate a range of memory int* pm_malloc(uint block_count); // allocate some blocks void pm_free(int* p); // free a var (if allocated with pm_malloc) From c0ed208cb562ced3d6622fe12b05dfd43e4a1c40 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 13 Dec 2021 09:42:11 +0100 Subject: [PATCH 27/38] Refactor & debug text --- drivers/vga.c | 3 ++- kernel/kernel.c | 4 ++++ kernel/memory.c | 6 +++--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/drivers/vga.c b/drivers/vga.c index 066c3b5..cdc53be 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -85,7 +85,7 @@ void printalign(char* str, int attribute_byte, enum align alignment) { unsigned int strlenbuf = strlen(str); if( !alignment || alignment == LEFT ) { - print(str, attribute_byte); + set_cursor_pos(0, cursor_row); } else if ( alignment == RIGHT ) { set_cursor_pos(MAX_COLS - strlenbuf, cursor_row); } else if ( alignment == MIDDLE ) { @@ -93,4 +93,5 @@ void printalign(char* str, int attribute_byte, enum align alignment) { } print(str, attribute_byte); + set_cursor_pos(0, cursor_row+1); } diff --git a/kernel/kernel.c b/kernel/kernel.c index b979f15..192ec27 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -27,6 +27,8 @@ void init() { // Memory allocation testing + printalign("-- PMM Tests --", DEFAULT_COLOR, MIDDLE); + println("THESE ALLOC SHOULD WORK:", 0xa0); for(int i=0; i < 4; i++) { block_alloc(i); @@ -39,6 +41,8 @@ void init() { block_free(2); // after this, allocation of 2nd block should work block_alloc(2); + printalign("-- End of PMM Tests --", DEFAULT_COLOR, MIDDLE); + char* strbuf = "Concat test: "; char* str2 = "Works!"; strbuf = strcat(strbuf, str2); diff --git a/kernel/memory.c b/kernel/memory.c index ad6744c..b169f5a 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -15,7 +15,7 @@ #define CHECK_BITMAP(map, idx) ((map) & (1<<(idx))) -#define BLOCK_TO_MEMP(idx) idx +#define BLOCK_TO_MEMP(idx) (int*)(PM_MEM_START + (idx*BLOCK_SIZE)) static int bitmap = 0; @@ -40,11 +40,11 @@ int* block_alloc(uint blockidx) { mod_bitmap(blockidx, 1); last_block = blockidx; - return 0; // placeholder + return BLOCK_TO_MEMP(blockidx); // placeholder } else { println("ERROR! Attemped to allocate non-free block.", 0x0c); - return -1; + return 0; } } From f9a48a35dfc739925dc73b79f046ac1a4baeac89 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 13 Dec 2021 09:54:10 +0100 Subject: [PATCH 28/38] Refactor & headers --- drivers/vga.c | 20 ++++++++++---------- drivers/vga.h | 4 ++-- kernel/memory.c | 9 +++++++++ kernel/memory.h | 2 +- lib/types.h | 3 +++ 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/drivers/vga.c b/drivers/vga.c index cdc53be..5687714 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -1,12 +1,13 @@ // VGA Graphics Library #include "vga.h" +#include "../lib/types.h" #include "../kernel/io.h" #include "../kernel/memory.h" #include "../lib/str.h" #include "../lib/strf.h" -static unsigned int cursor_row = 0; -static unsigned int cursor_col = 0; +static uint cursor_row = 0; +static uint cursor_col = 0; void vga_init() { // Disable cursor @@ -19,18 +20,17 @@ void vga_init() { set_cursor_pos(0, 0); - // TODO: allocate VGA physical memory - // pm_mem_alloc(VGA_ADDRESS, VGA_ADDRESS_MAX); + pm_alloc_range(VGA_ADDRESS, VGA_ADDRESS_MAX, true); // force alloc the VGA range } /* VGA & Memory Functions */ -char* get_memory_charpos(unsigned int col, unsigned int row) { +char* get_memory_charpos(uint col, uint row) { return (char*)(VGA_ADDRESS + 2*((row*80) + col)); } -void writechar(char c, unsigned int col, unsigned int row, int attribute_byte) { +void writechar(char c, uint col, uint row, int attribute_byte) { if( !attribute_byte ) attribute_byte = DEFAULT_COLOR; @@ -41,7 +41,7 @@ void writechar(char c, unsigned int col, unsigned int row, int attribute_byte) { } -void set_cursor_pos(unsigned int col, unsigned int row) { +void set_cursor_pos(uint col, uint row) { cursor_col = col; cursor_row = row; } @@ -50,7 +50,7 @@ void set_cursor_pos(unsigned int col, unsigned int row) { /* Graphics Functions */ -void clear_row(unsigned int row) { +void clear_row(uint row) { for( int c = 0; c < MAX_COLS; c++ ) writechar(0x20, c, row, 0x0); } @@ -66,7 +66,7 @@ void clear_screen() { */ void print(char* str, int attribute_byte) { for( char* c = str; *c != '\0'; c++ ) - writechar(*c, (unsigned int)(c - str) + cursor_col, cursor_row, attribute_byte); + writechar(*c, (uint)(c - str) + cursor_col, cursor_row, attribute_byte); } void println(char* str, int attribute_byte) { @@ -82,7 +82,7 @@ void printint(int i, int attribute_byte) { } void printalign(char* str, int attribute_byte, enum align alignment) { - unsigned int strlenbuf = strlen(str); + uint strlenbuf = strlen(str); if( !alignment || alignment == LEFT ) { set_cursor_pos(0, cursor_row); diff --git a/drivers/vga.h b/drivers/vga.h index f8f1d08..3777623 100644 --- a/drivers/vga.h +++ b/drivers/vga.h @@ -1,5 +1,5 @@ -#define VGA_ADDRESS (char*)0xb8000 -#define VGA_ADDRESS_MAX (char*)0xb8fa0 +#define VGA_ADDRESS 0xb8000 +#define VGA_ADDRESS_MAX 0xb8fa0 #define DEFAULT_COLOR 0x07 #define MAX_ROWS 25 diff --git a/kernel/memory.c b/kernel/memory.c index b169f5a..edc313c 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -54,6 +54,15 @@ void block_free(uint blockidx) { last_block = blockidx; } +void pm_alloc_range(uint start, uint end, bool force) { + uint idx_start; + uint idx_end; + + // calculate idx_start and idx_end + // if not force, check if avaliable + // allocate (if permitted) +} + /* int* pm_malloc(uint block_count) { diff --git a/kernel/memory.h b/kernel/memory.h index 431b8ca..156de0a 100644 --- a/kernel/memory.h +++ b/kernel/memory.h @@ -16,7 +16,7 @@ void mod_bitmap(uint bit, uint flag); int* block_alloc(uint blockidx); // allocate a block void block_free(uint blockidx); // free a block -//void pm_alloc_range(uint start, uint end, bool force); // allocate a range of memory +void pm_alloc_range(uint start, uint end, bool force); // allocate a range of memory int* pm_malloc(uint block_count); // allocate some blocks void pm_free(int* p); // free a var (if allocated with pm_malloc) diff --git a/lib/types.h b/lib/types.h index 9113551..970e402 100644 --- a/lib/types.h +++ b/lib/types.h @@ -1 +1,4 @@ typedef unsigned int uint; +typedef int bool; +#define true 1 +#define false 0 From dd4e773b81030af77cbc7587c2ffaa23ce820b45 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 20 Dec 2021 09:24:50 +0100 Subject: [PATCH 29/38] Pointer type --- kernel/memory.c | 8 ++++---- kernel/memory.h | 4 ++-- lib/types.h | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/kernel/memory.c b/kernel/memory.c index edc313c..a962e26 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -15,7 +15,7 @@ #define CHECK_BITMAP(map, idx) ((map) & (1<<(idx))) -#define BLOCK_TO_MEMP(idx) (int*)(PM_MEM_START + (idx*BLOCK_SIZE)) +#define BLOCK_TO_MEMP(idx) (pointer)(PM_MEM_START + (idx*BLOCK_SIZE)) static int bitmap = 0; @@ -30,7 +30,7 @@ void mod_bitmap(uint bit, uint bflag) { } -int* block_alloc(uint blockidx) { +pointer block_alloc(uint blockidx) { int block_bflag; block_bflag = CHECK_BITMAP(bitmap, blockidx); @@ -40,7 +40,7 @@ int* block_alloc(uint blockidx) { mod_bitmap(blockidx, 1); last_block = blockidx; - return BLOCK_TO_MEMP(blockidx); // placeholder + return BLOCK_TO_MEMP(blockidx); } else { println("ERROR! Attemped to allocate non-free block.", 0x0c); @@ -64,7 +64,7 @@ void pm_alloc_range(uint start, uint end, bool force) { } /* -int* pm_malloc(uint block_count) { +pointer pm_malloc(uint block_count) { } */ diff --git a/kernel/memory.h b/kernel/memory.h index 156de0a..e6695a4 100644 --- a/kernel/memory.h +++ b/kernel/memory.h @@ -13,10 +13,10 @@ void mod_bitmap(uint bit, uint flag); -int* block_alloc(uint blockidx); // allocate a block +pointer block_alloc(uint blockidx); // allocate a block void block_free(uint blockidx); // free a block void pm_alloc_range(uint start, uint end, bool force); // allocate a range of memory -int* pm_malloc(uint block_count); // allocate some blocks +pointer pm_malloc(uint block_count); // allocate some blocks void pm_free(int* p); // free a var (if allocated with pm_malloc) diff --git a/lib/types.h b/lib/types.h index 970e402..76e3a00 100644 --- a/lib/types.h +++ b/lib/types.h @@ -1,4 +1,5 @@ typedef unsigned int uint; +typedef unsigned long* pointer; typedef int bool; #define true 1 #define false 0 From 88565642d6301a646ca5e9ab41b08e2ad17a1e68 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 20 Dec 2021 09:45:59 +0100 Subject: [PATCH 30/38] VGA memory allocation --- kernel/memory.c | 36 ++++++++++++++++++++++++++++++++---- kernel/memory.h | 3 ++- lib/types.h | 1 + 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/kernel/memory.c b/kernel/memory.c index a962e26..51f193b 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -17,6 +17,7 @@ #define BLOCK_TO_MEMP(idx) (pointer)(PM_MEM_START + (idx*BLOCK_SIZE)) +#define MEMP_TO_BLOCK(memp) (uint)((memp - PM_MEM_START)/BLOCK_SIZE) static int bitmap = 0; static uint last_block; @@ -42,7 +43,7 @@ pointer block_alloc(uint blockidx) { return BLOCK_TO_MEMP(blockidx); } else { - println("ERROR! Attemped to allocate non-free block.", 0x0c); + println("[ERROR] Attemped to allocate non-free block.", 0x0c); return 0; } @@ -54,13 +55,40 @@ void block_free(uint blockidx) { last_block = blockidx; } -void pm_alloc_range(uint start, uint end, bool force) { +bool check_block_range(uint start, uint end) { + bool allowed = true; + + uint idx; + for(idx = start; idx <= end; idx++) { + if( CHECK_BITMAP(bitmap, idx) != BM_FREE ) + allowed = false; + break; + } + + return allowed; +} + +void pm_alloc_range(ulong start, ulong end, bool force) { uint idx_start; uint idx_end; - // calculate idx_start and idx_end - // if not force, check if avaliable + ulong d_addr = end - start; // memory size + uint num_blocks = (d_addr/BLOCK_SIZE) + 1; // amount of blocks to be allocated + + uint start_block = MEMP_TO_BLOCK(start); // start idx, end = start_block + num_blocks - 1 + bool allowed = true && check_block_range(start_block, start_block + num_blocks - 1); + // allocate (if permitted) + if( allowed ) { + uint idx; + for(idx=start_block; idx <= start_block + num_blocks - 1; idx++) + block_alloc(idx); + + return; + } else { + println("[ERROR] Tried to allocate memory range without permission!", 0x0c); + return; + } } /* diff --git a/kernel/memory.h b/kernel/memory.h index e6695a4..770e503 100644 --- a/kernel/memory.h +++ b/kernel/memory.h @@ -16,7 +16,8 @@ void mod_bitmap(uint bit, uint flag); pointer block_alloc(uint blockidx); // allocate a block void block_free(uint blockidx); // free a block -void pm_alloc_range(uint start, uint end, bool force); // allocate a range of memory +bool check_block_range(uint start, uint end); +void pm_alloc_range(ulong start, ulong end, bool force); // allocate a range of memory pointer pm_malloc(uint block_count); // allocate some blocks void pm_free(int* p); // free a var (if allocated with pm_malloc) diff --git a/lib/types.h b/lib/types.h index 76e3a00..d79be95 100644 --- a/lib/types.h +++ b/lib/types.h @@ -1,4 +1,5 @@ typedef unsigned int uint; +typedef unsigned long ulong; typedef unsigned long* pointer; typedef int bool; #define true 1 From 4eb396a6c718c68ba1756fcbb6490fd08d903ff2 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 20 Dec 2021 10:01:01 +0100 Subject: [PATCH 31/38] More bugs --- kernel/memory.c | 6 ++++-- kernel/memory.h | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/memory.c b/kernel/memory.c index 51f193b..7fd295a 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -55,6 +55,10 @@ void block_free(uint blockidx) { last_block = blockidx; } +uint find_free(uint block_count) { + // TODO: find a free start block to allocate +} + bool check_block_range(uint start, uint end) { bool allowed = true; @@ -91,8 +95,6 @@ void pm_alloc_range(ulong start, ulong end, bool force) { } } -/* pointer pm_malloc(uint block_count) { } -*/ diff --git a/kernel/memory.h b/kernel/memory.h index 770e503..f6fce88 100644 --- a/kernel/memory.h +++ b/kernel/memory.h @@ -16,6 +16,8 @@ void mod_bitmap(uint bit, uint flag); pointer block_alloc(uint blockidx); // allocate a block void block_free(uint blockidx); // free a block +uint find_free(uint block_count); + bool check_block_range(uint start, uint end); void pm_alloc_range(ulong start, ulong end, bool force); // allocate a range of memory From 903ec10e6c847f8c2f888837010dcf0e4059cc8c Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 21 Dec 2021 22:48:30 +0100 Subject: [PATCH 32/38] Added very helpful comments --- kernel/memory.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/memory.c b/kernel/memory.c index 7fd295a..20c9030 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -57,6 +57,8 @@ void block_free(uint blockidx) { uint find_free(uint block_count) { // TODO: find a free start block to allocate + // loop through bitmap + // check if range is free } bool check_block_range(uint start, uint end) { From 08d1f09e4cc3e5e8c4d19098dca36fd2414b8102 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 24 Dec 2021 01:07:06 +0100 Subject: [PATCH 33/38] Refactor --- lib/conv.c | 2 ++ lib/conv.h | 1 + lib/strf.c | 15 --------------- lib/strf.h | 1 - 4 files changed, 3 insertions(+), 16 deletions(-) create mode 100644 lib/conv.c create mode 100644 lib/conv.h delete mode 100644 lib/strf.c delete mode 100644 lib/strf.h diff --git a/lib/conv.c b/lib/conv.c new file mode 100644 index 0000000..5ed3295 --- /dev/null +++ b/lib/conv.c @@ -0,0 +1,2 @@ +#include "conv.h" + diff --git a/lib/conv.h b/lib/conv.h new file mode 100644 index 0000000..0c037b9 --- /dev/null +++ b/lib/conv.h @@ -0,0 +1 @@ +void int_to_str(int i, char* buf); diff --git a/lib/strf.c b/lib/strf.c deleted file mode 100644 index 65dddc7..0000000 --- a/lib/strf.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "strf.h" - -#define int_offset 48 -// 0:48 - 9:57 - -char* int_to_str(int i, char* strbuf) { - if( i == 0 ) { - return (char*)(int_offset); - } else { - char cbuf; - cbuf = (char)((i % 10) + int_offset); - - return int_to_str(i / 10, strbuf + cbuf); - } -} diff --git a/lib/strf.h b/lib/strf.h deleted file mode 100644 index 29649bc..0000000 --- a/lib/strf.h +++ /dev/null @@ -1 +0,0 @@ -char* int_to_str(int i, char* strbuf); From 62f473f1e7eb65ec8a766c8539d96370f8f442d8 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 24 Dec 2021 01:23:37 +0100 Subject: [PATCH 34/38] Conversion stuff --- drivers/vga.c | 4 +++- kernel/kernel.c | 2 +- lib/conv.c | 10 ++++++++++ lib/conv.h | 2 ++ lib/util.c | 10 ++++++++++ lib/util.h | 3 +++ 6 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 lib/util.c create mode 100644 lib/util.h diff --git a/drivers/vga.c b/drivers/vga.c index 5687714..333ef52 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -4,7 +4,7 @@ #include "../kernel/io.h" #include "../kernel/memory.h" #include "../lib/str.h" -#include "../lib/strf.h" +#include "../lib/conv.h" static uint cursor_row = 0; static uint cursor_col = 0; @@ -75,10 +75,12 @@ void println(char* str, int attribute_byte) { } void printint(int i, int attribute_byte) { + /* char* strbuf; strbuf = int_to_str(i, strbuf); println(strbuf, attribute_byte); + */ } void printalign(char* str, int attribute_byte, enum align alignment) { diff --git a/kernel/kernel.c b/kernel/kernel.c index 192ec27..446c01d 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -3,7 +3,7 @@ #include "paging.h" #include "../drivers/vga.h" #include "../lib/str.h" -#include "../lib/strf.h" +#include "../lib/conv.h" void init() { vga_init(); // Initialize the screen first diff --git a/lib/conv.c b/lib/conv.c index 5ed3295..8c9284c 100644 --- a/lib/conv.c +++ b/lib/conv.c @@ -1,2 +1,12 @@ #include "conv.h" +void int_to_str(int i, char* buf) { + bool use_sign = false; + + if( i < 0 ) { + i = (ulong)(i * (-1)) + use_sign = true; + } + + uint len = ulong_len(i); +} diff --git a/lib/conv.h b/lib/conv.h index 0c037b9..3e4e383 100644 --- a/lib/conv.h +++ b/lib/conv.h @@ -1 +1,3 @@ +#include "types.h" + void int_to_str(int i, char* buf); diff --git a/lib/util.c b/lib/util.c new file mode 100644 index 0000000..f534869 --- /dev/null +++ b/lib/util.c @@ -0,0 +1,10 @@ +#include "util.h" + +uint ulong_len(ulong i) { + int len = 0; + while (i != 0) { + i = i / 10; + ++len; + } + return len; +} diff --git a/lib/util.h b/lib/util.h new file mode 100644 index 0000000..6c4e531 --- /dev/null +++ b/lib/util.h @@ -0,0 +1,3 @@ +#include "types.h" + +uint ulong_len(ulong n); From f9ebd01832626c322f540af2a7e705695e1fa112 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 24 Dec 2021 01:43:22 +0100 Subject: [PATCH 35/38] Int to str --- lib/conv.c | 13 +++++++------ lib/conv.h | 2 ++ lib/util.c | 10 +++++++--- lib/util.h | 1 + 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/conv.c b/lib/conv.c index 8c9284c..731ea6a 100644 --- a/lib/conv.c +++ b/lib/conv.c @@ -1,12 +1,13 @@ #include "conv.h" void int_to_str(int i, char* buf) { - bool use_sign = false; + uint num = (uint)i; // convert to uint + uint len = ulong_len(num); // number of digits - if( i < 0 ) { - i = (ulong)(i * (-1)) - use_sign = true; - } + &buf[len] = '\0'; // add a "end-of-string" at the end + + int j; + for(j = 0; j < len; j++) // iterate over each digit and assign it to the buffer + &buf[j] = (char)(ndigit(num, len-1-j) + ASCII_OFFSET); // apply the ascii offset so that i becomes a char - uint len = ulong_len(i); } diff --git a/lib/conv.h b/lib/conv.h index 3e4e383..776644b 100644 --- a/lib/conv.h +++ b/lib/conv.h @@ -1,3 +1,5 @@ #include "types.h" +#define ASCII_OFFSET 0x30 + void int_to_str(int i, char* buf); diff --git a/lib/util.c b/lib/util.c index f534869..4690128 100644 --- a/lib/util.c +++ b/lib/util.c @@ -1,10 +1,14 @@ #include "util.h" -uint ulong_len(ulong i) { +uint ulong_len(ulong n) { // get the digit length of a number int len = 0; - while (i != 0) { - i = i / 10; + while (n != 0) { + n = n / 10; ++len; } return len; } + +uint ndigit(ulong n, uint i) { // OBS: index order is reversed + return (n/(10**i)) % 10 +} diff --git a/lib/util.h b/lib/util.h index 6c4e531..2cebf96 100644 --- a/lib/util.h +++ b/lib/util.h @@ -1,3 +1,4 @@ #include "types.h" uint ulong_len(ulong n); +uint ndigit(ulong n, uint i); From 40b8aa7a3d2645751a9bb4f2b129f0380878cb1d Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 24 Dec 2021 01:45:14 +0100 Subject: [PATCH 36/38] Comp errors fixed -> more comp errors :( --- lib/conv.c | 2 +- lib/conv.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/conv.c b/lib/conv.c index 731ea6a..501df93 100644 --- a/lib/conv.c +++ b/lib/conv.c @@ -1,7 +1,7 @@ #include "conv.h" void int_to_str(int i, char* buf) { - uint num = (uint)i; // convert to uint + ulong num = (ulong)i; // convert to ulong uint len = ulong_len(num); // number of digits &buf[len] = '\0'; // add a "end-of-string" at the end diff --git a/lib/conv.h b/lib/conv.h index 776644b..42becfc 100644 --- a/lib/conv.h +++ b/lib/conv.h @@ -1,4 +1,5 @@ #include "types.h" +#include "util.h" #define ASCII_OFFSET 0x30 From 45ff8a854fd8633b2a6512c876ce69819ef981e4 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 24 Dec 2021 13:03:44 +0100 Subject: [PATCH 37/38] Added math library --- lib/conv.c | 5 +++-- lib/math.c | 10 ++++++++++ lib/math.h | 3 +++ lib/util.c | 3 ++- 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 lib/math.c create mode 100644 lib/math.h diff --git a/lib/conv.c b/lib/conv.c index 501df93..b9654c5 100644 --- a/lib/conv.c +++ b/lib/conv.c @@ -4,10 +4,11 @@ void int_to_str(int i, char* buf) { ulong num = (ulong)i; // convert to ulong uint len = ulong_len(num); // number of digits - &buf[len] = '\0'; // add a "end-of-string" at the end + *(buf+len) = '\0'; // add a "end-of-string" at the end int j; for(j = 0; j < len; j++) // iterate over each digit and assign it to the buffer - &buf[j] = (char)(ndigit(num, len-1-j) + ASCII_OFFSET); // apply the ascii offset so that i becomes a char + // super dangerous memory write + *(buf+j) = (char)(ndigit(num, len-1-j) + ASCII_OFFSET); // apply the ascii offset so that i becomes a char } diff --git a/lib/math.c b/lib/math.c new file mode 100644 index 0000000..fc4415a --- /dev/null +++ b/lib/math.c @@ -0,0 +1,10 @@ +#include "math.h" + +long pow(int num, uint expon) { + long prod = 1; + while(expon > 0) + prod *= num; + --expon; + + return prod; +} diff --git a/lib/math.h b/lib/math.h new file mode 100644 index 0000000..1abe7e4 --- /dev/null +++ b/lib/math.h @@ -0,0 +1,3 @@ +#include "types.h" + +long pow(int, uint); diff --git a/lib/util.c b/lib/util.c index 4690128..efdd22d 100644 --- a/lib/util.c +++ b/lib/util.c @@ -10,5 +10,6 @@ uint ulong_len(ulong n) { // get the digit length of a number } uint ndigit(ulong n, uint i) { // OBS: index order is reversed - return (n/(10**i)) % 10 + long den = pow(10, i); + return (n/den) % 10; } From fd7e32e141cb0012e55a3f05178619b1bd387cd9 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 24 Dec 2021 13:13:12 +0100 Subject: [PATCH 38/38] Minor refactor --- drivers/vga.c | 5 +++-- drivers/vga.h | 3 +++ kernel/kernel.c | 11 ++++++++++- kernel/memory.c | 2 +- lib/conv.c | 7 +++++-- lib/conv.h | 2 +- lib/math.c | 2 +- lib/util.c | 1 + 8 files changed, 25 insertions(+), 8 deletions(-) diff --git a/drivers/vga.c b/drivers/vga.c index 333ef52..8821fe7 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -10,6 +10,9 @@ static uint cursor_row = 0; static uint cursor_col = 0; void vga_init() { + // Allocate VGA memory range + pm_alloc_range(VGA_ADDRESS, VGA_ADDRESS_MAX, true); // force alloc the VGA range + // Disable cursor port_outb(0x3d4, 0x0a); port_outb(0x3d5, 0x20); @@ -19,8 +22,6 @@ void vga_init() { clear_screen(); set_cursor_pos(0, 0); - - pm_alloc_range(VGA_ADDRESS, VGA_ADDRESS_MAX, true); // force alloc the VGA range } /* diff --git a/drivers/vga.h b/drivers/vga.h index 3777623..0ba4f1f 100644 --- a/drivers/vga.h +++ b/drivers/vga.h @@ -2,6 +2,9 @@ #define VGA_ADDRESS_MAX 0xb8fa0 #define DEFAULT_COLOR 0x07 +#define URGET_COLOR 0x0c +#define SUCCESS_COLOR 0x0a + #define MAX_ROWS 25 #define MAX_COLS 80 diff --git a/kernel/kernel.c b/kernel/kernel.c index 446c01d..2005a4d 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -9,7 +9,7 @@ void init() { vga_init(); // Initialize the screen first // i.e. clear the screen et cetera. - println("Kernel loaded", DEFAULT_COLOR); + println("Kernel loaded", SUCCESS_COLOR); // enable_paging(); @@ -26,6 +26,14 @@ void init() { */ + char* intbuf = "xxxx"; + int num = 1234; + intbuf = int_to_str(num, intbuf); + print("TEST NUM: ", DEFAULT_COLOR); + println(intbuf, DEFAULT_COLOR); + + /* + // Memory allocation testing printalign("-- PMM Tests --", DEFAULT_COLOR, MIDDLE); @@ -47,4 +55,5 @@ void init() { char* str2 = "Works!"; strbuf = strcat(strbuf, str2); println(strbuf, DEFAULT_COLOR); + */ } diff --git a/kernel/memory.c b/kernel/memory.c index 20c9030..265121e 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -36,7 +36,7 @@ pointer block_alloc(uint blockidx) { block_bflag = CHECK_BITMAP(bitmap, blockidx); if( block_bflag == BM_FREE ) { // check if block is free - println("Alloc!", DEFAULT_COLOR); + println("Allocating block...", DEFAULT_COLOR); mod_bitmap(blockidx, 1); last_block = blockidx; diff --git a/lib/conv.c b/lib/conv.c index b9654c5..2ba09b2 100644 --- a/lib/conv.c +++ b/lib/conv.c @@ -1,6 +1,7 @@ #include "conv.h" +#include "../drivers/vga.h" -void int_to_str(int i, char* buf) { +char* int_to_str(int i, char* buf) { ulong num = (ulong)i; // convert to ulong uint len = ulong_len(num); // number of digits @@ -9,6 +10,8 @@ void int_to_str(int i, char* buf) { int j; for(j = 0; j < len; j++) // iterate over each digit and assign it to the buffer // super dangerous memory write + println("char!", DEFAULT_COLOR); *(buf+j) = (char)(ndigit(num, len-1-j) + ASCII_OFFSET); // apply the ascii offset so that i becomes a char - + + return buf; } diff --git a/lib/conv.h b/lib/conv.h index 42becfc..8c14438 100644 --- a/lib/conv.h +++ b/lib/conv.h @@ -3,4 +3,4 @@ #define ASCII_OFFSET 0x30 -void int_to_str(int i, char* buf); +char* int_to_str(int i, char* buf); diff --git a/lib/math.c b/lib/math.c index fc4415a..bfded6f 100644 --- a/lib/math.c +++ b/lib/math.c @@ -4,7 +4,7 @@ long pow(int num, uint expon) { long prod = 1; while(expon > 0) prod *= num; - --expon; + expon--; return prod; } diff --git a/lib/util.c b/lib/util.c index efdd22d..ba9b2d2 100644 --- a/lib/util.c +++ b/lib/util.c @@ -1,4 +1,5 @@ #include "util.h" +#include "math.h" uint ulong_len(ulong n) { // get the digit length of a number int len = 0;