--- /dev/null
+; 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
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);
+}
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);