]> git.dujemihanovic.xyz Git - nameless-os.git/commitdiff
Get memory map from BIOS E820
authorDuje Mihanović <duje.mihanovic@skole.hr>
Fri, 17 Jun 2022 18:47:51 +0000 (20:47 +0200)
committerDuje Mihanović <duje.mihanovic@skole.hr>
Wed, 22 Jun 2022 16:03:18 +0000 (18:03 +0200)
The entry order is reversed, but otherwise the map makes sense. Also print the
map out to screen.

boot/x86/stage3/e820.s [new file with mode: 0644]
boot/x86/stage3/loader.s
include/arch/x86/tty.h
kernel/arch/x86/tty/tty.c
kernel/entry.s
kernel/kernel.c

diff --git a/boot/x86/stage3/e820.s b/boot/x86/stage3/e820.s
new file mode 100644 (file)
index 0000000..247b361
--- /dev/null
@@ -0,0 +1,65 @@
+; Code for getting the BIOS E820 memory map
+
+bits 16
+
+; Return: ECX - number of entries, DI - pointer to map
+get_e820_map:
+       push eax
+       push ebx
+       push edx
+       push esi
+       push es
+
+       xor ax, ax
+       mov es, ax
+       push dword 1
+       sub sp, 20
+       xor si, si
+       xor ebx, ebx
+       mov edx, 0x534d4150
+       mov eax, 0xe820
+       mov ecx, 24
+       mov di, sp
+       int 0x15
+       jc .no_e820
+       cmp eax, 0x534d4150
+       jne .unexpected
+       inc si
+       .loop:
+               push dword 1
+               sub sp, 20
+               mov di, sp
+               mov eax, 0xe820
+               mov ecx, 24
+               int 0x15
+               jc .carry_done
+               inc si
+               cmp ebx, 0
+               je .done
+               jmp .loop
+       .carry_done:
+               add sp, 24
+       .done:
+               movzx ecx, si
+               movzx edi, sp
+               mov ax, 24
+               mul si
+               add sp, ax
+               pop es
+               pop esi
+               pop edx
+               pop ebx
+               pop eax
+               ret
+       .no_e820:
+               print no_e820
+               hlt
+               jmp $-1
+       .unexpected
+               print e820_unexpected
+               call print_dword
+               hlt
+               jmp $-1
+
+no_e820: db "BIOS does not support E820, cannot proceed!", 0xd, 0xa, 0
+e820_unexpected: db "Unexpected value in EAX after getting map entry: expected SMAP, got ", 0
index 2967c6ee7b1d9f00c1d9fb1ef732f02a10c3847e..0ca0ea6012591eaea6b0c573bfb7edb961b65eb1 100644 (file)
@@ -64,6 +64,8 @@ _start:
        call read_clus_chain_unreal ; load kernel
        print kernel_loaded
        
+       call get_e820_map
+
        cli
        lgdt [gdt]
        mov eax, cr0
@@ -151,6 +153,7 @@ memcpy:
 %include "../fat32/fat32.s"
 %include "gdt.s"
 %include "print.s"
+%include "e820.s"
 
 in_protected:
 bits 32
index 22247379ede32e1615192c1558f5bc05318991b0..ce49da9b178e10f98495accdaeef8f2c2da91b8f 100644 (file)
@@ -25,4 +25,5 @@ extern void kprint(const char *string, uint8_t color);
 extern void kprintb(const uint8_t byte);
 extern void kprintw(const uint16_t word);
 extern void kprintd(const uint32_t dword);
+extern void kprintq(const uint64_t qword);
 #endif
index 07d734e54fc00f1919e51e0efe624271158c2e01..2110a7d003703dc3e0d2f89c483521889e2f33d7 100644 (file)
@@ -137,3 +137,41 @@ void kprintd(uint32_t dword)
        temp = dword & 0xF;
        kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
 }
+
+void kprintq(uint64_t qword)
+{
+       uint8_t temp;
+       temp = qword >> 60;
+       kprint("0x", VGA_COLOR_LIGHT_GRAY);
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 56) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 52) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 48) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 44) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 40) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 36) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 32) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 28) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 24) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 20) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 16) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 12) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 8) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = (qword >> 4) & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+       temp = qword & 0xF;
+       kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
+}
index 10c95e8985955184b0b8f0afde73f5c99c692d13..5ef71bb84d95dc517fa994c60b4b7a8f12e5aad0 100644 (file)
@@ -6,6 +6,8 @@ global _start
 _start:
        mov ebp, __STACK_BOTTOM__
        mov esp, ebp
+       push ecx
+       push edi
        call kmain
        hlt
        jmp $-1
index 060051c25782f3317f426f0ea44362e420a98b80..0aa8fc260ddfd604789fa6e2ff0050cb2f4a8970 100644 (file)
@@ -11,13 +11,38 @@ extern void pf_handler(struct fault_frame *frame);
 static struct idt_descriptor idt[256] __attribute__((aligned(0x10)));
 static struct idtr idtr __attribute__((aligned(0x10)));
 
+struct e820_map {
+       uint64_t base;
+       uint64_t length;
+       uint32_t type;
+       uint32_t attrib;
+};
 
-void kmain(void)
+void print_e820(struct e820_map *mmap, int mmap_size)
+{
+       kprint("       Base       |      Length      |   Type   |  Attrib  |\n", 0);
+       kprint("------------------------------------------------------------\n", 0);
+       for (int i=0; i<mmap_size; i++) {
+               kprintq(mmap[i].base);
+               kprintc('|', 0);
+               kprintq(mmap[i].length);
+               kprintc('|', 0);
+               kprintd(mmap[i].type);
+               kprintc('|', 0);
+               kprintd(mmap[i].attrib);
+               kprintc('|', 0);
+               kprintc('\n', 0);
+       }
+}
+
+void kmain(struct e820_map *mmap, int mmap_size)
 {
        screen_clear();
        kprint("Welcome to Nameless OS!\nRunning revision: ", 0);
        kprint(GIT_COMMIT, 0);
        kprint("\n", 0);
+       kprint("BIOS memory map:\n", 0);
+       print_e820(mmap, mmap_size);
        kprint("Preparing IDT...\n", 0);
        idt_set_descriptor(idt, 8, 0x8, (uint32_t) double_fault, IDT_INTERRUPT_GATE, 0x0);
        idt_set_descriptor(idt, 14, 0x8, (uint32_t) pf_handler, IDT_INTERRUPT_GATE, 0x0);