From d79fc6e8e2a6f0ffca741463d1021dff86e5dc58 Mon Sep 17 00:00:00 2001 From: Nigel Date: Wed, 3 Nov 2021 20:03:38 +0100 Subject: [PATCH] Split up boot.s into multiple assembly definitions, Started page frame allocator implementation, kterm definition is now considered c plus plus --- Makefile | 9 +- src/kernel/arch/i386/boot.S | 89 +++ src/kernel/arch/i386/boot.s | 566 ------------------ src/kernel/arch/i386/gdt/gdt.s | 37 ++ src/kernel/arch/i386/idt/idt.h | 4 +- src/kernel/arch/i386/idt/idt.s | 6 + src/kernel/arch/i386/irq_table.s | 138 +++++ src/kernel/arch/i386/irs_table.s | 256 ++++++++ src/kernel/arch/i386/paging.s | 20 + .../arch/i386/tty/{kterm.c => kterm.cpp} | 4 +- src/kernel/arch/i386/tty/kterm.h | 5 +- src/kernel/bootcheck.h | 4 +- src/kernel/kernel.cpp | 56 +- src/kernel/kernel.h | 103 +--- src/kernel/memory/PageFramAllocator.cpp | 1 - src/kernel/memory/PageFrameAllocator.cpp | 38 ++ src/kernel/memory/PageFrameAllocator.h | 14 +- src/kernel/serial.h | 70 +++ 18 files changed, 695 insertions(+), 725 deletions(-) create mode 100644 src/kernel/arch/i386/boot.S delete mode 100644 src/kernel/arch/i386/boot.s create mode 100644 src/kernel/arch/i386/gdt/gdt.s create mode 100644 src/kernel/arch/i386/idt/idt.s create mode 100644 src/kernel/arch/i386/irq_table.s create mode 100644 src/kernel/arch/i386/irs_table.s create mode 100644 src/kernel/arch/i386/paging.s rename src/kernel/arch/i386/tty/{kterm.c => kterm.cpp} (97%) delete mode 100644 src/kernel/memory/PageFramAllocator.cpp create mode 100644 src/kernel/memory/PageFrameAllocator.cpp create mode 100644 src/kernel/serial.h diff --git a/Makefile b/Makefile index da1419f..e3010f6 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CC = ${HOME}/opt/cross/bin/i686-elf-gcc CPP = ${HOME}/opt/cross/bin/i686-elf-g++ CFLAGS = -ffreestanding -O2 -Wall -Wextra -OFILES = $(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/io.o $(BUILD_DIR)/PageDirectory.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/string.o +OFILES = $(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/PageFrameAllocator.o $(BUILD_DIR)/io.o $(BUILD_DIR)/PageDirectory.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/string.o SRC_DIR = src BUILD_DIR = build @@ -57,10 +57,10 @@ $(BUILD_DIR)/kernel.o: $(CPP) -c $(SRC_DIR)/kernel/kernel.cpp -o $(BUILD_DIR)/kernel.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/kterm.o: - $(CC) -c $(SRC_DIR)/kernel/arch/i386/tty/kterm.c -o $(BUILD_DIR)/kterm.o $(CFLAGS) -std=gnu99 + $(CPP) -c $(SRC_DIR)/kernel/arch/i386/tty/kterm.cpp -o $(BUILD_DIR)/kterm.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/boot.o: - $(AS) $(SRC_DIR)/kernel/arch/i386/boot.s -o $(BUILD_DIR)/boot.o + $(AS) $(SRC_DIR)/kernel/arch/i386/boot.S -o $(BUILD_DIR)/boot.o $(BUILD_DIR)/crti.o: $(AS) $(SRC_DIR)/kernel/arch/i386/crti.s -o $(BUILD_DIR)/crti.o @@ -82,3 +82,6 @@ $(BUILD_DIR)/pic.o: $(BUILD_DIR)/string.o: $(CC) -c $(SRC_DIR)/libc/include/string.c -o $(BUILD_DIR)/string.o $(CFLAGS) -std=gnu99 + +$(BUILD_DIR)/PageFrameAllocator.o: + $(CPP) -c $(SRC_DIR)/kernel/memory/PageFrameAllocator.cpp -o $(BUILD_DIR)/PageFrameAllocator.o $(CFLAGS) -fno-exceptions -fno-rtti diff --git a/src/kernel/arch/i386/boot.S b/src/kernel/arch/i386/boot.S new file mode 100644 index 0000000..5f5ba72 --- /dev/null +++ b/src/kernel/arch/i386/boot.S @@ -0,0 +1,89 @@ +/* + * Multiboot + */ +.set ALIGN, 1<<0 /* align loaded modules on page boundaries */ +.set MEMINFO, 1<<1 /* provide memory map */ +.set FLAGS, ALIGN | MEMINFO /* this is the Multiboot 'flag' field */ +.set MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */ +.set CHECKSUM, -(MAGIC + FLAGS) /* checksum of above, to prove we are multiboot */ + +.section .multiboot +.align 4 +.long MAGIC +.long FLAGS +.long CHECKSUM + + +.section .bss +.align 16 +stack_bottom: +.skip 16384 # 16 KiB +stack_top: + +.section .text +.include "./src/kernel/arch/i386/irs_table.s" +.include "./src/kernel/arch/i386/irq_table.s" +.include "./src/kernel/arch/i386/idt/idt.s" +.include "./src/kernel/arch/i386/paging.s" + + +.global _start +.type _start, @function +_start: + /*Setup the stack pointer to point to the beginning of our stack */ + /* I believe its a high address growing down to lower adress for the stack on x86*/ + mov $stack_top, %esp + + /*Reset EFLAGS*/ + pushl $0 + popf + + /* push the pointer to the Multiboot information structure*/ + pushl %ebx + + /* push the magic value */ + pushl %eax + + call early_main + cli + + load_gdt: + lgdt gdt + + # set the segment selecters + + movw $0x10, %ax + movw %ax, %ds + movw %ax, %es + movw %ax, %fs + movw %ax, %gs + movw %ax, %ss + ljmp $0x08, $flush + + flush: + + + #load idt + call init_idt + sti + + # Try enable A20 + # mov $0x2401, %ax + # int $0x15 + + + # enable protected mode + mov %cr0, %eax + or $1, %eax + mov %eax, %cr0 + + + call kernel_main + + + cli + 1: hlt + jmp 1b + + +.include "./src/kernel/arch/i386/gdt/gdt.s" diff --git a/src/kernel/arch/i386/boot.s b/src/kernel/arch/i386/boot.s deleted file mode 100644 index 096450a..0000000 --- a/src/kernel/arch/i386/boot.s +++ /dev/null @@ -1,566 +0,0 @@ -/* - * Multiboot - */ -.set ALIGN, 1<<0 /* align loaded modules on page boundaries */ -.set MEMINFO, 1<<1 /* provide memory map */ -.set FLAGS, ALIGN | MEMINFO /* this is the Multiboot 'flag' field */ -.set MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */ -.set CHECKSUM, -(MAGIC + FLAGS) /* checksum of above, to prove we are multiboot */ - -.section .multiboot -.align 4 -.long MAGIC -.long FLAGS -.long CHECKSUM - - -.section .bss -.align 16 -stack_bottom: -.skip 16384 # 16 KiB -stack_top: - -.section .text -/* -* Interupt handlers -*/ -# NOTE: I have no clue how I should use these macros. -# Have tried to use them in a myriad of ways, none would actually work -.macro irs_NoErrCode code:req - .globl irs\code - irs\code: - cli - pushb $0 - pushb \code - jmp irs_common -.endm - -.macro irs_ErrCode code - .globl irs\code - irs\code: - cli - pushb \code - jmp irs_common -.endm - -.globl irs0 -irs0: - cli - push $0 - push $0 - jmp irs_common - -.globl irs1 -irs1: - cli - push $0 - push $1 - jmp irs_common - -.globl irs2 -irs2: - cli - push $0 - push $2 - jmp irs_common - -.globl irs3 -irs3: - cli - push $0 - push $3 - jmp irs_common - -.globl irs4 -irs4: - cli - push $0 - push $4 - jmp irs_common - -.globl irs5 -irs5: - cli - push $0 - push $5 - jmp irs_common - -.globl irs6 -irs6: - cli - push $0 - push $6 - jmp irs_common - -.globl irs7 -irs7: - cli - push $0 - push $7 - jmp irs_common - -.globl irs8 -irs8: - cli - push $0 - push $8 - jmp irs_common - -.globl irs9 -irs9: - cli - push $0 - push $9 - jmp irs_common - -.globl irs10 -irs10: - cli - push $0 - push $10 - jmp irs_common - -.globl irs11 -irs11: - cli - push $0 - push $11 - jmp irs_common - -.globl irs12 -irs12: - cli - push $0 - push $12 - jmp irs_common - -.globl irs13 -irs13: - cli - push $13 - jmp irs_common - -.globl irs14 -irs14: - cli - push $0 - push $14 - jmp irs_common - -.globl irs15 -irs15: - cli - push $0 - push $15 - jmp irs_common - -.globl irs16 -irs16: - cli - push $0 - push $16 - jmp irs_common - -.globl irs17 -irs17: - cli - push $0 - push $17 - jmp irs_common - -.globl irs18 -irs18: - cli - push $0 - push $18 - jmp irs_common - -.globl irs19 -irs19: - cli - push $0 - push $19 - jmp irs_common - -.globl irs20 -irs20: - cli - push $0 - push $20 - jmp irs_common - -.globl irs21 -irs21: - cli - push $0 - push $21 - jmp irs_common - -.globl irs22 -irs22: - cli - push $0 - push $22 - jmp irs_common - -.globl irs23 -irs23: - cli - push $0 - push $23 - jmp irs_common - -.globl irs24 -irs24: - cli - push $0 - push $24 - jmp irs_common - -.globl irs25 -irs25: - cli - push $0 - push $25 - jmp irs_common - -.globl irs26 -irs26: - cli - push $0 - push $26 - jmp irs_common - -.globl irs27 -irs27: - cli - push $0 - push $27 - jmp irs_common - -.globl irs28 -irs28: - cli - push $0 - push $28 - jmp irs_common - -.globl irs29 -irs29: - cli - push $0 - push $29 - jmp irs_common - -.globl irs30 -irs30: - cli - push $0 - push $30 - jmp irs_common - -.globl irs31 -irs31: - cli - push $0 - push $31 - jmp irs_common - - - -.globl irq0 -irq0: - cli - push $0 - push $0 - jmp irq_common - -.globl irq1 -irq1: - cli - push $0 - push $1 - jmp irq_common - -.globl irq2 -irq2: - cli - push $0 - push $2 - jmp irq_common - -.globl irq3 -irq3: - cli - push $0 - push $3 - jmp irq_common - -.globl irq4 -irq4: - cli - push $0 - push $4 - jmp irq_common - -.globl irq5 -irq5: - cli - push $0 - push $5 - jmp irq_common - -.globl irq6 -irq6: - cli - push $0 - push $6 - jmp irq_common - -.globl irq7 -irq7: - cli - push $0 - push $7 - jmp irq_common - -.globl irq8 -irq8: - cli - push $0 - push $8 - jmp irq_common - -.globl irq9 -irq9: - cli - push $0 - push $9 - jmp irq_common - -.globl irq10 -irq10: - cli - push $0 - push $10 - jmp irq_common - -.globl irq11 -irq11: - cli - push $0 - push $11 - jmp irq_common - - -.globl irq12 -irq12: - cli - push $0 - push $12 - jmp irq_common - -.globl irq13 -irq13: - cli - push $0 - push $13 - jmp irq_common - -.globl irq14 -irq14: - cli - push $0 - push $14 - jmp irq_common - -.globl irq15 -irq15: - cli - push $0 - push $15 - jmp irq_common - -irq_common: - pusha - - mov %ds, %ax - push %eax - - mov $0x10, %ax - mov %ax, %ds - mov %ax, %es - mov %ax, %fs - mov %ax, %gs - - call irq_handler - - pop %eax - - mov %ax, %ds - mov %ax, %es - mov %ax, %fs - mov %ax, %gs - - popa - add $8, %esp # cleans push error and irs code - sti - iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP - - -irs_common: - pusha # Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax - - - mov %ds, %ax - push %eax - - /* load the kernel data segment descriptor*/ - - mov $0x10, %ax - mov %ax, %ds - mov %ax, %es - mov %ax, %fs - mov %ax, %gs - - call irs_handler - - pop %eax - - mov %ax, %ds - mov %ax, %es - mov %ax, %fs - mov %ax, %gs - - popa - add $8, %esp # cleans push error and irs code - sti - iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP - -.globl idt_flush -idt_flush: - mov 4(%esp), %eax - lidt (%eax) - ret - -.globl enablePaging -enablePaging: - push %ebp - mov %esp, %ebp - mov %cr0, %eax - or $0x80000000, %eax - mov %eax, %cr0 - mov %ebp, %esp - pop %ebp - ret - -.globl loadPageDirectory -loadPageDirectory: - push %ebp - mov %esp, %ebp - mov 8(%esp), %eax - mov %eax, %cr3 - mov %ebp, %esp - pop %ebp - ret - -.global _start -.type _start, @function -_start: - /*Setup the stack pointer to point to the beginning of our stack */ - /* I believe its a high address growing down to lower adress for the stack on x86*/ - mov $stack_top, %esp - - /*Reset EFLAGS*/ - pushl $0 - popf - - /* push the pointer to the Multiboot information structure*/ - pushl %ebx - - /* push the magic value */ - pushl %eax - - call early_main - cli - - load_gdt: - lgdt gdt - - # set the segment selecters - - movw $0x10, %ax - movw %ax, %ds - movw %ax, %es - movw %ax, %fs - movw %ax, %gs - movw %ax, %ss - ljmp $0x08, $flush - - flush: - - - #load idt - call init_idt - sti - - # Try enable A20 - # mov $0x2401, %ax - # int $0x15 - - - # enable protected mode - mov %cr0, %eax - or $1, %eax - mov %eax, %cr0 - - - call kernel_main - - - cli - 1: hlt - jmp 1b - - - - - /* Tell processor to use our gdt*/ - gdt: - .word (gdt_end - gdt_start -1) # Size of the GDT in bytes minus 1 for math reasons - .int gdt_start # linear address of our GDT - - - - -.att_syntax - - -.size _start, . - _start - - -/* -* Create the GDT -*/ -.section .data -gdt_start: -gdt_null: -.long 0x0 -.long 0x0 - -gdt_kcode: -.word 0xFFFF # limit -.word 0x0 # base -.byte 0x0 # base -.byte 0b10011010 # 1st flags | type flags -.byte 0b11001111 # 2nd flags | limit -.byte 0x0 # base - -gdt_kdata: -.word 0xFFFF # limit -.word 0x0 # base -.byte 0x0 # base -.byte 0b10010010 # 1st flags | type flags -.byte 0b11001111 # 2nd flags | limit -.byte 0x0 # base -gdt_end: diff --git a/src/kernel/arch/i386/gdt/gdt.s b/src/kernel/arch/i386/gdt/gdt.s new file mode 100644 index 0000000..066cd1f --- /dev/null +++ b/src/kernel/arch/i386/gdt/gdt.s @@ -0,0 +1,37 @@ +/* Tell processor to use our gdt*/ +gdt: + .word (gdt_end - gdt_start -1) # Size of the GDT in bytes minus 1 for math reasons + .int gdt_start # linear address of our GDT + + +.att_syntax + + +.size _start, . - _start + + +/* +* Create the GDT +*/ +.section .data +gdt_start: +gdt_null: +.long 0x0 +.long 0x0 + +gdt_kcode: +.word 0xFFFF # limit +.word 0x0 # base +.byte 0x0 # base +.byte 0b10011010 # 1st flags | type flags +.byte 0b11001111 # 2nd flags | limit +.byte 0x0 # base + +gdt_kdata: +.word 0xFFFF # limit +.word 0x0 # base +.byte 0x0 # base +.byte 0b10010010 # 1st flags | type flags +.byte 0b11001111 # 2nd flags | limit +.byte 0x0 # base +gdt_end: diff --git a/src/kernel/arch/i386/idt/idt.h b/src/kernel/arch/i386/idt/idt.h index cedb1ff..04173d3 100644 --- a/src/kernel/arch/i386/idt/idt.h +++ b/src/kernel/arch/i386/idt/idt.h @@ -5,9 +5,7 @@ #include "../vga/colors.h" #include "../pic/pic.h" -extern "C"{ - #include "../tty/kterm.h" -} +#include "../tty/kterm.h" extern "C" { diff --git a/src/kernel/arch/i386/idt/idt.s b/src/kernel/arch/i386/idt/idt.s new file mode 100644 index 0000000..5e19912 --- /dev/null +++ b/src/kernel/arch/i386/idt/idt.s @@ -0,0 +1,6 @@ + +.globl idt_flush +idt_flush: + mov 4(%esp), %eax + lidt (%eax) + ret diff --git a/src/kernel/arch/i386/irq_table.s b/src/kernel/arch/i386/irq_table.s new file mode 100644 index 0000000..fac477d --- /dev/null +++ b/src/kernel/arch/i386/irq_table.s @@ -0,0 +1,138 @@ +.globl irq0 +irq0: + cli + push $0 + push $0 + jmp irq_common + +.globl irq1 +irq1: + cli + push $0 + push $1 + jmp irq_common + +.globl irq2 +irq2: + cli + push $0 + push $2 + jmp irq_common + +.globl irq3 +irq3: + cli + push $0 + push $3 + jmp irq_common + +.globl irq4 +irq4: + cli + push $0 + push $4 + jmp irq_common + +.globl irq5 +irq5: + cli + push $0 + push $5 + jmp irq_common + +.globl irq6 +irq6: + cli + push $0 + push $6 + jmp irq_common + +.globl irq7 +irq7: + cli + push $0 + push $7 + jmp irq_common + +.globl irq8 +irq8: + cli + push $0 + push $8 + jmp irq_common + +.globl irq9 +irq9: + cli + push $0 + push $9 + jmp irq_common + +.globl irq10 +irq10: + cli + push $0 + push $10 + jmp irq_common + +.globl irq11 +irq11: + cli + push $0 + push $11 + jmp irq_common + + +.globl irq12 +irq12: + cli + push $0 + push $12 + jmp irq_common + +.globl irq13 +irq13: + cli + push $0 + push $13 + jmp irq_common + +.globl irq14 +irq14: + cli + push $0 + push $14 + jmp irq_common + +.globl irq15 +irq15: + cli + push $0 + push $15 + jmp irq_common + +irq_common: + pusha + + mov %ds, %ax + push %eax + + mov $0x10, %ax + mov %ax, %ds + mov %ax, %es + mov %ax, %fs + mov %ax, %gs + + call irq_handler + + pop %eax + + mov %ax, %ds + mov %ax, %es + mov %ax, %fs + mov %ax, %gs + + popa + add $8, %esp # cleans push error and irs code + sti + iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP diff --git a/src/kernel/arch/i386/irs_table.s b/src/kernel/arch/i386/irs_table.s new file mode 100644 index 0000000..8e7c6de --- /dev/null +++ b/src/kernel/arch/i386/irs_table.s @@ -0,0 +1,256 @@ +/* +* Interupt handlers +*/ + +.globl irs0 +irs0: + cli + push $0 + push $0 + jmp irs_common + +.globl irs1 +irs1: + cli + push $0 + push $1 + jmp irs_common + +.globl irs2 +irs2: + cli + push $0 + push $2 + jmp irs_common + +.globl irs3 +irs3: + cli + push $0 + push $3 + jmp irs_common + +.globl irs4 +irs4: + cli + push $0 + push $4 + jmp irs_common + +.globl irs5 +irs5: + cli + push $0 + push $5 + jmp irs_common + +.globl irs6 +irs6: + cli + push $0 + push $6 + jmp irs_common + +.globl irs7 +irs7: + cli + push $0 + push $7 + jmp irs_common + +.globl irs8 +irs8: + cli + push $0 + push $8 + jmp irs_common + +.globl irs9 +irs9: + cli + push $0 + push $9 + jmp irs_common + +.globl irs10 +irs10: + cli + push $0 + push $10 + jmp irs_common + +.globl irs11 +irs11: + cli + push $0 + push $11 + jmp irs_common + +.globl irs12 +irs12: + cli + push $0 + push $12 + jmp irs_common + +.globl irs13 +irs13: + cli + push $13 + jmp irs_common + +.globl irs14 +irs14: + cli + push $0 + push $14 + jmp irs_common + +.globl irs15 +irs15: + cli + push $0 + push $15 + jmp irs_common + +.globl irs16 +irs16: + cli + push $0 + push $16 + jmp irs_common + +.globl irs17 +irs17: + cli + push $0 + push $17 + jmp irs_common + +.globl irs18 +irs18: + cli + push $0 + push $18 + jmp irs_common + +.globl irs19 +irs19: + cli + push $0 + push $19 + jmp irs_common + +.globl irs20 +irs20: + cli + push $0 + push $20 + jmp irs_common + +.globl irs21 +irs21: + cli + push $0 + push $21 + jmp irs_common + +.globl irs22 +irs22: + cli + push $0 + push $22 + jmp irs_common + +.globl irs23 +irs23: + cli + push $0 + push $23 + jmp irs_common + +.globl irs24 +irs24: + cli + push $0 + push $24 + jmp irs_common + +.globl irs25 +irs25: + cli + push $0 + push $25 + jmp irs_common + +.globl irs26 +irs26: + cli + push $0 + push $26 + jmp irs_common + +.globl irs27 +irs27: + cli + push $0 + push $27 + jmp irs_common + +.globl irs28 +irs28: + cli + push $0 + push $28 + jmp irs_common + +.globl irs29 +irs29: + cli + push $0 + push $29 + jmp irs_common + +.globl irs30 +irs30: + cli + push $0 + push $30 + jmp irs_common + +.globl irs31 +irs31: + cli + push $0 + push $31 + jmp irs_common + + +irs_common: + pusha # Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax + + + mov %ds, %ax + push %eax + + /* load the kernel data segment descriptor*/ + + mov $0x10, %ax + mov %ax, %ds + mov %ax, %es + mov %ax, %fs + mov %ax, %gs + + call irs_handler + + pop %eax + + mov %ax, %ds + mov %ax, %es + mov %ax, %fs + mov %ax, %gs + + popa + add $8, %esp # cleans push error and irs code + sti + iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP diff --git a/src/kernel/arch/i386/paging.s b/src/kernel/arch/i386/paging.s new file mode 100644 index 0000000..fa25003 --- /dev/null +++ b/src/kernel/arch/i386/paging.s @@ -0,0 +1,20 @@ +.globl enablePaging +enablePaging: + push %ebp + mov %esp, %ebp + mov %cr0, %eax + or $0x80000000, %eax + mov %eax, %cr0 + mov %ebp, %esp + pop %ebp + ret + +.globl loadPageDirectory +loadPageDirectory: + push %ebp + mov %esp, %ebp + mov 8(%esp), %eax + mov %eax, %cr3 + mov %ebp, %esp + pop %ebp + ret diff --git a/src/kernel/arch/i386/tty/kterm.c b/src/kernel/arch/i386/tty/kterm.cpp similarity index 97% rename from src/kernel/arch/i386/tty/kterm.c rename to src/kernel/arch/i386/tty/kterm.cpp index f4bc957..94bc3f1 100644 --- a/src/kernel/arch/i386/tty/kterm.c +++ b/src/kernel/arch/i386/tty/kterm.cpp @@ -202,10 +202,8 @@ void printf ( const char *format, ...) { switch (c) { case 'd': - kterm_writestring("Not implemented!!"); - break; + case 'u': - break; case 'x': itoa(buf, c, *((int *) arg++)); diff --git a/src/kernel/arch/i386/tty/kterm.h b/src/kernel/arch/i386/tty/kterm.h index b43faa9..7d2ace6 100644 --- a/src/kernel/arch/i386/tty/kterm.h +++ b/src/kernel/arch/i386/tty/kterm.h @@ -7,8 +7,9 @@ #include "../vga/colors.h" #include "../../../io.h" - -#include "./../../../../libc/include/string.h" +extern "C"{ + #include "./../../../../libc/include/string.h" +} void kterm_init(); diff --git a/src/kernel/bootcheck.h b/src/kernel/bootcheck.h index 890c4f8..c7b7796 100644 --- a/src/kernel/bootcheck.h +++ b/src/kernel/bootcheck.h @@ -2,9 +2,7 @@ #include "bootloader/multiboot.h" #define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit))) -extern "C" { - #include "arch/i386/tty/kterm.h" -} +#include "arch/i386/tty/kterm.h" diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index a04f977..f3658b2 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -1,8 +1,7 @@ #include "kernel.h" -#include "arch/i386/gdt/gdtc.h" -extern "C" { +#include "memory/PageFrameAllocator.h" - void early_main(unsigned long magic, unsigned long addr){ + extern "C" void early_main(unsigned long magic, unsigned long addr){ /** initialize terminal interface */ kterm_init(); @@ -13,58 +12,13 @@ extern "C" { CheckMBT( (multiboot_info_t *) addr); - multiboot_info_t* mbt = (multiboot_info_t*) addr; - // Map the kernel - //initPhysicalMemoryManager(); - - // AAAAAH memory map, Yes please! /* Are mmap_* valid? */ if (CHECK_FLAG(mbt->flags, 6)){ - multiboot_memory_map_t *mmap = (multiboot_memory_map_t*) mbt->mmap_addr; - uint32_t memorySizeInBytes = 0; - uint32_t reservedMemoryInBytes = 0; + mapMultibootMemoryMap(mbt); - - printf("mmap_addr = 0x%x, mmap_length = 0x%x\n", - (unsigned) mbt->mmap_addr, (unsigned) mbt->mmap_length); - - for (; (unsigned long) mmap < mbt->mmap_addr + mbt->mmap_length; mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size))){ - if ( mmap->type == MULTIBOOT_MEMORY_AVAILABLE){ - memorySizeInBytes += mmap->len; - } else { - reservedMemoryInBytes += mmap->len; - } - - - printf( - "size = 0x%x, base_addr = 0x%x%08x, length = 0x%x%08x, type = 0x%x\n", - (unsigned) mmap->size, - (unsigned) (mmap->addr >> 32), - (unsigned) (mmap->addr & 0xffffffff), - (unsigned) (mmap->len >> 32), - (unsigned) (mmap->len & 0xffffffff), - (unsigned) mmap->type); - - } - uint32_t memorySizeInGiB = memorySizeInBytes / 1073741824; - - printf("Available Memory: 0x%x bytes, 0x%x GiB\n", memorySizeInBytes, memorySizeInGiB ); - printf("Reserved Memory: 0x%x bytes\n", reservedMemoryInBytes ); } - - - - //int cpu_model = get_model(); - //int local_apic = check_apic(); - //printf( "CPU Model: %x, Local APIC: %D\n", cpu_model, local_apic); - - - /* Setup Paging and memory Managment*/ - //MMU MemoryManagementUnit = MMU(); - //MemoryManagementUnit.enable(); // Warning: Causes triple page fault - //printf("Pages available: %9d\n", pmm_available()); /* Draw diagonal blue line */ if (CHECK_FLAG (mbt->flags, 12)){ @@ -76,7 +30,7 @@ extern "C" { } - void kernel_main (void) { + extern "C" void kernel_main (void) { init_serial(); @@ -88,4 +42,4 @@ extern "C" { } } -} + diff --git a/src/kernel/kernel.h b/src/kernel/kernel.h index 445e8fa..bf2fdd9 100644 --- a/src/kernel/kernel.h +++ b/src/kernel/kernel.h @@ -1,19 +1,24 @@ -#pragma once -extern "C" { - #include "arch/i386/tty/kterm.h" +#pragma once +extern "C"{ + #include "../libc/include/string.h" } +#include "arch/i386/vga/VBE.h" +#include "arch/i386/tty/kterm.h" -#include "../libc/include/string.h" #include "./bootloader/multiboot.h" + #include "bootcheck.h" + +#include "arch/i386/gdt/gdtc.h" #include "arch/i386/idt/idt.h" #include "io.h" #include "time.h" #include "cpu.h" -#include "arch/i386/vga/VBE.h" -#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit))) +#include "serial.h" +#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit))) +#define PANIC(message) { return; } /* This needs to be moved! */ @@ -26,89 +31,3 @@ void delay(int t){ for(j=0;j<25000;j++) asm("NOP"); } - -class Test { - public: - Test(); - void printMe(); - ~Test(); -}; - -Test::Test(){ - kterm_writestring("Create a test object\n"); -}; - -void Test::printMe(){ - kterm_writestring("testObject.printMe()\n"); -} - -Test::~Test(){ - kterm_writestring("Destroy testObject! Bye bye\n"); -} - -#define PORT 0x3f8 -static int init_serial() { - outb(PORT + 1, 0x00); // Disable all interrupts - outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor) - outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud - outb(PORT + 1, 0x00); // (hi byte) - outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit - outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold - outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set - outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip - outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte) - - // Check if serial is faulty (i.e: not same byte as sent) - if(inb(PORT + 0) != 0xAE) { - return 1; - } - - // If serial is not faulty set it in normal operation mode - // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) - outb(PORT + 4, 0x0F); - return 0; -} - -int is_transmit_empty() { - return inb(PORT + 5) & 0x20; -} - -void write_serial(char a) { - while (is_transmit_empty() == 0); - - outb(PORT,a); -} - -int serial_received() { - return inb(PORT + 5) & 1; -} - -char read_serial() { - while (serial_received() == 0); - - return inb(PORT); -} - -void print_serial(const char* string ){ - for(size_t i = 0; i < strlen(string); i ++){ - write_serial(string[i]); - } -} - -void test_serial(){ - /** Serial test **/ - kterm_writestring("Writing to COM1 serial port:"); - init_serial(); - write_serial('A'); - write_serial('B'); - write_serial('C'); - write_serial('D'); - write_serial('E'); - - char Character_received = read_serial(); - kterm_writestring("\n"); - kterm_writestring("received from COM 1: \n"); - kterm_put(Character_received); - - kterm_writestring("\n"); -} diff --git a/src/kernel/memory/PageFramAllocator.cpp b/src/kernel/memory/PageFramAllocator.cpp deleted file mode 100644 index b690689..0000000 --- a/src/kernel/memory/PageFramAllocator.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "PageFrameAllocator.h" diff --git a/src/kernel/memory/PageFrameAllocator.cpp b/src/kernel/memory/PageFrameAllocator.cpp new file mode 100644 index 0000000..f34c282 --- /dev/null +++ b/src/kernel/memory/PageFrameAllocator.cpp @@ -0,0 +1,38 @@ +#include "PageFrameAllocator.h" + + +MemoryInfo memInfo {}; +void mapMultibootMemoryMap( multiboot_info_t *mbt){ + printf("mmap_addr = 0x%x, mmap_length = 0x%x\n", + (unsigned) mbt->mmap_addr, (unsigned) mbt->mmap_length); + multiboot_memory_map_t *mmap = (multiboot_memory_map_t*) mbt->mmap_addr; + + for (; (unsigned long) mmap < mbt->mmap_addr + mbt->mmap_length; mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size))){ + + if ( mmap->type == MULTIBOOT_MEMORY_AVAILABLE){ + memInfo.memorySizeInBytes += mmap->len; + } else { + memInfo.reservedMemoryInBytes += mmap->len; + } + + print_Multiboot_memory_Map(mmap); + + } + uint32_t memorySizeInGiB = memInfo.memorySizeInBytes / 1073741824; + + printf("Available Memory: 0x%x bytes, 0x%x GiB\n", memInfo.memorySizeInBytes, memorySizeInGiB ); + printf("Reserved Memory: 0x%x bytes\n", memInfo.reservedMemoryInBytes ); + +} + +void print_Multiboot_memory_Map(multiboot_memory_map_t* mmap){ + printf( + "size = 0x%x, base_addr = 0x%x%08x, length = 0x%x%08x, type = 0x%x\n", + (unsigned) mmap->size, + (unsigned) (mmap->addr >> 32), + (unsigned) (mmap->addr & 0xffffffff), + (unsigned) (mmap->len >> 32), + (unsigned) (mmap->len & 0xffffffff), + (unsigned) mmap->type + ); +} \ No newline at end of file diff --git a/src/kernel/memory/PageFrameAllocator.h b/src/kernel/memory/PageFrameAllocator.h index 56d4871..2b19180 100644 --- a/src/kernel/memory/PageFrameAllocator.h +++ b/src/kernel/memory/PageFrameAllocator.h @@ -1,8 +1,20 @@ #pragma once -#include +#include "../arch/i386/tty/kterm.h" +#include +#include "../bootloader/multiboot.h" + +struct MemoryInfo{ + uint32_t memorySizeInBytes = 0; + uint32_t reservedMemoryInBytes = 0; +}; extern void *kernel_begin; extern void *kernel_end; + + + +void print_Multiboot_memory_Map(multiboot_memory_map_t*); +void mapMultibootMemoryMap(multiboot_info_t*); \ No newline at end of file diff --git a/src/kernel/serial.h b/src/kernel/serial.h new file mode 100644 index 0000000..b14db96 --- /dev/null +++ b/src/kernel/serial.h @@ -0,0 +1,70 @@ +#pragma once + +#include "arch/i386/tty/kterm.h" +#include "io.h" +#define PORT 0x3f8 +static int init_serial() { + outb(PORT + 1, 0x00); // Disable all interrupts + outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor) + outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud + outb(PORT + 1, 0x00); // (hi byte) + outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit + outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold + outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set + outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip + outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte) + + // Check if serial is faulty (i.e: not same byte as sent) + if(inb(PORT + 0) != 0xAE) { + return 1; + } + + // If serial is not faulty set it in normal operation mode + // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) + outb(PORT + 4, 0x0F); + return 0; +} + +int is_transmit_empty() { + return inb(PORT + 5) & 0x20; +} + +void write_serial(char a) { + while (is_transmit_empty() == 0); + + outb(PORT,a); +} + +int serial_received() { + return inb(PORT + 5) & 1; +} + +char read_serial() { + while (serial_received() == 0); + + return inb(PORT); +} + +void print_serial(const char* string ){ + for(size_t i = 0; i < strlen(string); i ++){ + write_serial(string[i]); + } +} + +void test_serial(){ + /** Serial test **/ + kterm_writestring("Writing to COM1 serial port:"); + init_serial(); + write_serial('A'); + write_serial('B'); + write_serial('C'); + write_serial('D'); + write_serial('E'); + + char Character_received = read_serial(); + kterm_writestring("\n"); + kterm_writestring("received from COM 1: \n"); + kterm_put(Character_received); + + kterm_writestring("\n"); +}