Upgrade to multiboot version 2
authorcoderain <coderain@sdf.org>
Sat, 10 Feb 2018 23:45:33 +0000 (00:45 +0100)
committercoderain <coderain@sdf.org>
Sat, 10 Feb 2018 23:45:33 +0000 (00:45 +0100)
grub.cfg
kernel/include/boot/multiboot.h
kernel/include/memory.h
kernel/src/boot/boot.asm
kernel/src/memory/memory.c
kernel/src/start.c

index e0910851f5ddc0f203e73cd33853a10da7c7ee86..ab2849c550b8fe8855e92e10f357423195513f2a 100644 (file)
--- a/grub.cfg
+++ b/grub.cfg
@@ -1,3 +1,3 @@
 menuentry "Monolithium OS" {
-    multiboot /boot/monolithium manager:Floppy0/MANAGER
+    multiboot2 /boot/monolithium manager:Floppy0/MANAGER
 }
index c2395efa8cf88a1b1d307356bd4b01afb8f6a17b..021ac7a50118902f2079324aea2133efc8b1f2a8 100644 (file)
 
 typedef struct
 {
-    dword_t flags;
-    dword_t mem_lower, mem_upper;
-    dword_t boot_device;
-    dword_t cmdline;
-    dword_t mods_count, mods_addr;
-    dword_t syms[4];
-    dword_t mmap_length, mmap_addr;
-    dword_t drives_length, drives_addr;
-    dword_t config_table;
-    dword_t boot_loader_name;
-    dword_t apm_table;
-    dword_t vbe_control_info, vbe_mode_info, vbe_mode, vbe_interface_seg, vbe_interface_off, vbe_interface_len;
+    dword_t size;
+    dword_t reserved;
 } multiboot_header_t;
 
+typedef enum
+{
+    MULTIBOOT_INFO_END = 0,
+
+    MULTIBOOT_INFO_CMDLINE,
+    MULTIBOOT_INFO_LOADER,
+    MULTIBOOT_INFO_MODULES,
+    MULTIBOOT_INFO_MEMORY,
+    MULTIBOOT_INFO_BOOT_DEVICE,
+    MULTIBOOT_INFO_MEMORY_MAP,
+    MULTIBOOT_INFO_VBE_INFO,
+    MULTIBOOT_INFO_FRAMEBUFFER,
+    MULTIBOOT_INFO_SYMBOLS,
+    MULTIBOOT_INFO_APM,
+    MULTIBOOT_INFO_EFI32,
+    MULTIBOOT_INFO_EFI64,
+    MULTIBOOT_INFO_SMBIOS,
+    MULTIBOOT_INFO_ACPI1_RSDP,
+    MULTIBOOT_INFO_ACPI2_RSDP,
+    MULTIBOOT_INFO_NETWORK,
+    MULTIBOOT_INFO_EFI_MEMORY_MAP,
+    MULTIBOOT_INFO_EFI_SERVICES,
+    MULTIBOOT_INFO_EFI32_IMAGE_HANDLE,
+    MULTIBOOT_INFO_EFI64_IMAGE_HANDLE,
+    MULTIBOOT_INFO_BASE_ADDRESS,
+} multiboot_info_type_t;
+
+typedef struct
+{
+    dword_t type;
+    dword_t size;
+} multiboot_tag_t;
+
+typedef struct
+{
+    dword_t type;
+    dword_t size;
+    char string[VARIABLE_SIZE];
+} multiboot_tag_cmdline_t, multiboot_tag_loader_t;
+
 typedef struct
 {
+    dword_t type;
     dword_t size;
+    dword_t mod_start;
+    dword_t mod_end;
+    char string[VARIABLE_SIZE];
+} multiboot_tag_module_t;
+
+typedef struct
+{
+    dword_t type;
+    dword_t size;
+    dword_t mem_lower;
+    dword_t mem_upper;
+} multiboot_tag_memory_t;
+
+typedef struct
+{
+    dword_t type;
+    dword_t size;
+    dword_t bios_device;
+    dword_t partition;
+    dword_t sub_partition;
+} multiboot_tag_boot_device_t;
+
+typedef struct
+{
     union
     {
-        struct
-        {
-            dword_t base_low, base_high;
-        };
+        struct { dword_t base_low, base_high; };
         qword_t base;
     };
+
+    union
+    {
+        struct { dword_t length_low, length_high; };
+        qword_t length;
+    };
+
+    dword_t type;
+    dword_t reserved;
+} multiboot_mmap_entry_t;
+
+typedef struct
+{
+    dword_t type;
+    dword_t size;
+    dword_t entry_size;
+    dword_t entry_version;
+} multiboot_tag_mmap_t;
+
+typedef struct
+{
+    dword_t type;
+    dword_t size;
+    word_t vbe_mode;
+    word_t vbe_interface_seg;
+    word_t vbe_interface_off;
+    word_t vbe_interface_Len;
+    byte_t vbe_control_info[512];
+    byte_t vbe_mode_info[256];
+} multiboot_tag_vbe_t;
+
+typedef struct
+{
+    dword_t type;
+    dword_t size;
+    qword_t fb_address;
+    dword_t fb_pitch;
+    dword_t fb_width;
+    dword_t fb_height;
+    byte_t fb_bpp;
+    byte_t fb_type;
+    byte_t fb_reserved;
+
     union
     {
         struct
         {
-            dword_t length_low, length_high;
+            dword_t palette_size;
+
+            struct
+            {
+                byte_t red, green, blue;
+            } palette[VARIABLE_SIZE];
+        };
+
+        struct
+        {
+            byte_t red_field_pos;
+            byte_t red_mask_size;
+            byte_t green_field_pos;
+            byte_t green_mask_size;
+            byte_t blue_field_pos;
+            byte_t blue_mask_size;
         };
-        qword_t length;
     };
-    dword_t type;
-} multiboot_mmap_t;
+} multiboot_tag_framebuffer_t;
 
 #endif
index 0392118b31a57d44e5cb1aa8dbcef93af383e018..2b2c1beae01f427ccf52d40bfe726044c3ac0adb 100644 (file)
@@ -188,7 +188,7 @@ dword_t clone_address_space(memory_address_space_t *original, memory_address_spa
 void bump_address_space(memory_address_space_t *mem_space);
 void delete_address_space(memory_address_space_t *mem_space);
 bool_t memory_fault_handler(void *address, registers_t *regs);
-void memory_init(multiboot_mmap_t *mmap_addr, dword_t mmap_length);
+void memory_init(multiboot_tag_mmap_t *mmap);
 void memory_cleanup(object_t *obj);
 
 #endif
index b6086119af10baeca4dda00124d3f517955ff504..e87ab80f80828a73c74542ac93ab85b7cbbd8dbb 100644 (file)
@@ -25,83 +25,102 @@ extern bss
 extern end
 extern kernel_main
 
-PHYSICAL_ADDRESS        EQU 0x100000
-VIRTUAL_ADDRESS         EQU 0x80000000
-STACK_SIZE              EQU 0x10000
-PAGE_DIR_SELF_ENTRY     EQU 768
+MULTIBOOT_MAGIC         EQU     0xE85250D6
+PHYSICAL_ADDRESS        EQU     0x100000
+VIRTUAL_ADDRESS         EQU     0x80000000
+STACK_SIZE              EQU     0x10000
+PAGE_DIR_SELF_ENTRY     EQU     768
 
-multiboot:              DD 0x1BADB002
-                        DD 0x03
-                        DD 0xE4524FFB
-                        DD multiboot
-                        DD code
-                        DD bss
-                        DD end
-                        DD start
+multiboot:              DD      MULTIBOOT_MAGIC
+                        DD      0
+                        DD      start - multiboot
+                        DD      multiboot - start - MULTIBOOT_MAGIC
+
+info_tag:               DW      1                           ; Information request tag
+                        DW      0
+                        DD      20
+                        DD      1                           ; Boot command line
+                        DD      3                           ; Boot modules
+                        DD      6                           ; Memory map
+
+                        DD      0                           ; Padding
+
+address_tag:            DW      2                           ; Address tag
+                        DW      0
+                        DD      24
+                        DD      multiboot - VIRTUAL_ADDRESS + PHYSICAL_ADDRESS
+                        DD      code - VIRTUAL_ADDRESS + PHYSICAL_ADDRESS
+                        DD      bss - VIRTUAL_ADDRESS + PHYSICAL_ADDRESS
+                        DD      end - VIRTUAL_ADDRESS + PHYSICAL_ADDRESS
+
+entry_tag:              DW      3                           ; Entry address tag
+                        DW      0
+                        DD      12
+                        DD      start - VIRTUAL_ADDRESS + PHYSICAL_ADDRESS
 
 bits 32
 
-start:                  mov ebp, end
-                        add ebp, PHYSICAL_ADDRESS - VIRTUAL_ADDRESS + STACK_SIZE + 0xFFF
-                        and ebp, 0xFFFFF000
-                        xor eax, eax
-                        mov edi, ebp
-                        mov ecx, ebp
-                        add ecx, 0x3FFFFF
-                        and ecx, 0xFFC00000
-                        add ecx, ebp
-                        add ecx, 0x3FFFFF - PHYSICAL_ADDRESS
-                        and ecx, 0xFFC00000
-                        shr ecx, 12
-                        add ecx, 0x400
+start:                  mov     ebp, end
+                        add     ebp, PHYSICAL_ADDRESS - VIRTUAL_ADDRESS + STACK_SIZE + 0xFFF
+                        and     ebp, 0xFFFFF000
+                        xor     eax, eax
+                        mov     edi, ebp
+                        mov     ecx, ebp
+                        add     ecx, 0x3FFFFF
+                        and     ecx, 0xFFC00000
+                        add     ecx, ebp
+                        add     ecx, 0x3FFFFF - PHYSICAL_ADDRESS
+                        and     ecx, 0xFFC00000
+                        shr     ecx, 12
+                        add     ecx, 0x400
                         cld
                         rep stosd
-                        mov dword [ebp + PAGE_DIR_SELF_ENTRY * 4], ebp
-                        or dword [ebp + PAGE_DIR_SELF_ENTRY * 4], 3
-                        mov eax, ebp
-                        add eax, 0x1003
-                        mov edi, ebp
-                        mov ecx, ebp
-                        add ecx, 0x3FFFFF
-                        shr ecx, 22
+                        mov     dword [ebp + PAGE_DIR_SELF_ENTRY * 4], ebp
+                        or      dword [ebp + PAGE_DIR_SELF_ENTRY * 4], 3
+                        mov     eax, ebp
+                        add     eax, 0x1003
+                        mov     edi, ebp
+                        mov     ecx, ebp
+                        add     ecx, 0x3FFFFF
+                        shr     ecx, 22
 .fill_directory_low:    stosd
-                        add eax, 0x1000
-                        loop .fill_directory_low
-                        lea edi, [ebp + 0x800]
-                        mov ecx, ebp
-                        add ecx, 0x3FFFFF
-                        shr ecx, 22
+                        add     eax, 0x1000
+                        loop    .fill_directory_low
+                        lea     edi, [ebp + 0x800]
+                        mov     ecx, ebp
+                        add     ecx, 0x3FFFFF
+                        shr     ecx, 22
 .fill_directory_high:   stosd
-                        add eax, 0x1000
-                        loop .fill_directory_high
-                        lea edi, [ebp + 0x1000]
-                        mov eax, 3
+                        add     eax, 0x1000
+                        loop    .fill_directory_high
+                        lea     edi, [ebp + 0x1000]
+                        mov     eax, 3
 .fill_tables:           stosd
-                        add eax, 0x1000
-                        cmp eax, ebp
-                        jb .fill_tables
-                        lea esi, [ebp + 0x1000 + (PHYSICAL_ADDRESS >> 12) * 4]
-                        mov edi, dword [ebp + 0x800]
-                        and edi, 0xFFFFF000
-                        mov ecx, ebp
-                        sub ecx, PHYSICAL_ADDRESS
-                        shr ecx, 12
+                        add     eax, 0x1000
+                        cmp     eax, ebp
+                        jb      .fill_tables
+                        lea     esi, [ebp + 0x1000 + (PHYSICAL_ADDRESS >> 12) * 4]
+                        mov     edi, dword [ebp + 0x800]
+                        and     edi, 0xFFFFF000
+                        mov     ecx, ebp
+                        sub     ecx, PHYSICAL_ADDRESS
+                        shr     ecx, 12
                         rep movsd
-.done:                  mov cr3, ebp
-                        mov eax, cr4
-                        or eax, 0x80
-                        mov cr4, eax
-                        mov eax, cr0
-                        or eax, 0x80010000
-                        mov cr0, eax
-                        mov eax, .continue
-                        jmp eax
-.continue:              mov esp, end
-                        add esp, STACK_SIZE
-                        mov ebp, esp
-                        push esp
-                        push ebx
-                        call kernel_main
-.return:                add esp, 8
+.done:                  mov     cr3, ebp
+                        mov     eax, cr4
+                        or      eax, 0x80
+                        mov     cr4, eax
+                        mov     eax, cr0
+                        or      eax, 0x80010000
+                        mov     cr0, eax
+                        mov     eax, .continue
+                        jmp     eax
+.continue:              mov     esp, end
+                        add     esp, STACK_SIZE
+                        mov     ebp, esp
+                        push    esp
+                        push    ebx
+                        call    kernel_main
+.return:                add     esp, 8
                         cli
                         hlt
index 15a3fc9ff68bc6ced54410430452a14ef27ae02a..e63b67b181a281a2702a1a8e6b0da0344049466e 100644 (file)
@@ -731,36 +731,36 @@ static void *create_page_directory(void)
     return directory;
 }
 
-static void fix_overlapping_sections(multiboot_mmap_t *mmap_addr, dword_t mmap_length)
+static void fix_overlapping_sections(multiboot_tag_mmap_t *mmap)
 {
-    multiboot_mmap_t *mmap = mmap_addr;
+    multiboot_mmap_entry_t *entry;
 
-    while ((dword_t)mmap < (dword_t)mmap_addr + mmap_length)
+    for (entry = (multiboot_mmap_entry_t*)(mmap + 1);
+         (uintptr_t)entry < ((uintptr_t)mmap + mmap->size);
+         entry = (multiboot_mmap_entry_t*)((uintptr_t)entry + mmap->entry_size))
     {
-        multiboot_mmap_t *ptr = (multiboot_mmap_t*)mmap_addr;
+        multiboot_mmap_entry_t *ptr;
 
-        while ((dword_t)ptr < (dword_t) mmap)
+        for (ptr = (multiboot_mmap_entry_t*)(mmap + 1);
+             (uintptr_t)ptr < (uintptr_t)entry;
+             ptr = (multiboot_mmap_entry_t*)((uintptr_t)ptr + mmap->entry_size))
         {
-            qword_t mmap_end = mmap->base + mmap->length;
+            qword_t entry_end = entry->base + entry->length;
             qword_t ptr_end = ptr->base + ptr->length;
 
-            if (mmap->base > ptr->base && mmap->base < ptr_end)
+            if (entry->base > ptr->base && entry->base < ptr_end)
             {
-                mmap->base = ptr_end;
-                if (mmap->base >= mmap_end) mmap->length = 0;
-                else mmap->length = mmap_end - mmap->base;
+                entry->base = ptr_end;
+                if (entry->base >= entry_end) entry->length = 0;
+                else entry->length = entry_end - entry->base;
             }
-            else if (ptr->base > mmap->base && ptr->base < mmap_end)
+            else if (ptr->base > entry->base && ptr->base < entry_end)
             {
-                ptr->base = mmap_end;
+                ptr->base = entry_end;
                 if (ptr->base >= ptr_end) ptr->length = 0;
                 else ptr->length = ptr_end - ptr->base;
             }
-
-            ptr = (multiboot_mmap_t*)((dword_t)ptr + ptr->size + sizeof(dword_t));
         }
-
-        mmap = (multiboot_mmap_t*)((dword_t)mmap + mmap->size + sizeof(dword_t));
     }
 }
 
@@ -2585,36 +2585,39 @@ bool_t memory_fault_handler(void *address, registers_t *regs)
     return FALSE;
 }
 
-void memory_init(multiboot_mmap_t *mmap_addr, dword_t mmap_length)
+void memory_init(multiboot_tag_mmap_t *mmap)
 {
     dword_t i, j;
-    multiboot_mmap_t *mmap = mmap_addr;
     dword_t *page_directory = (dword_t*)PAGE_DIRECTORY_ADDR;
 
-    fix_overlapping_sections(mmap_addr, mmap_length);
+    fix_overlapping_sections(mmap);
 
     puts("\nMemory map:\n\nBase\t\t\tLength\t\t\tType");
     puts("------------------------------------------------------------");
 
-    while ((dword_t)mmap < (dword_t)mmap_addr + mmap_length)
+    multiboot_mmap_entry_t *entry;
+
+    for (entry = (multiboot_mmap_entry_t*)(mmap + 1);
+         (uintptr_t)entry < ((uintptr_t)mmap + mmap->size);
+         entry = (multiboot_mmap_entry_t*)((uintptr_t)entry + mmap->entry_size))
     {
         printf("0x%08X%08X\t0x%08X%08X\t%s\n",
-               mmap->base_high,
-               mmap->base_low,
-               mmap->length_high,
-               mmap->length_low,
-               (mmap->type == 1) ? "Usable" : "Not Usable");
-
-        if (mmap->type == 1
-            && mmap->base_high == 0
-            && mmap->length_high == 0
-            && mmap->length_low < (0xFFFFFFFF - mmap->base_low)
-            && mmap->length_low > 0)
+               entry->base_high,
+               entry->base_low,
+               entry->length_high,
+               entry->length_low,
+               (entry->type == 1) ? "Usable" : "Not Usable");
+
+        if (entry->type == 1
+            && entry->base_high == 0
+            && entry->length_high == 0
+            && entry->length_low < (0xFFFFFFFF - entry->base_low)
+            && entry->length_low > 0)
         {
-            dword_t start_addr = mmap->base_low;
+            dword_t start_addr = entry->base_low;
             if (start_addr < MEM_FIRST_PHYS_ADDR) start_addr = MEM_FIRST_PHYS_ADDR;
             start_addr = PAGE_ALIGN_UP(start_addr);
-            dword_t end_addr = PAGE_ALIGN_UP(mmap->base_low + mmap->length_low);
+            dword_t end_addr = PAGE_ALIGN_UP(entry->base_low + entry->length_low);
             dword_t page = end_addr - PAGE_SIZE;
 
             while (page >= start_addr)
@@ -2647,8 +2650,6 @@ void memory_init(multiboot_mmap_t *mmap_addr, dword_t mmap_length)
                 page -= PAGE_SIZE;
             }
         }
-
-        mmap = (multiboot_mmap_t*)((dword_t)mmap + mmap->size + sizeof(dword_t));
     }
 
     puts("------------------------------------------------------------");
index 93b367191ff34c792e1e93d40e5a28429c882ac5..b9d9714f126e5047075334e2873c69352af54ef4 100644 (file)
@@ -78,8 +78,26 @@ void kernel_main(multiboot_header_t *mboot, dword_t stack)
 {
     int i = 0;
     const char *manager_path = "";
-    char cmdline[MAX_CMDLINE_LENGTH] = { '\0' };
-    void *cmdline_phys = (void*)mboot->cmdline;
+    char cmdline[MAX_CMDLINE_LENGTH] = "";
+
+    multiboot_tag_t *tag;
+    multiboot_tag_mmap_t *mmap = NULL;
+
+    for (tag = (multiboot_tag_t*)(mboot + 1);
+         tag->type != MULTIBOOT_INFO_END;
+         tag = (multiboot_tag_t*)(((uintptr_t)tag + tag->size + 7) & ~7))
+    {
+        switch (tag->type)
+        {
+        case MULTIBOOT_INFO_CMDLINE:
+            strncpy(cmdline, ((multiboot_tag_cmdline_t*)tag)->string, sizeof(cmdline));
+            break;
+
+        case MULTIBOOT_INFO_MEMORY_MAP:
+            mmap = (multiboot_tag_mmap_t*)tag;
+            break;
+        }
+    }
 
     cpu_init();
     segments_init();
@@ -94,7 +112,8 @@ void kernel_main(multiboot_header_t *mboot, dword_t stack)
     puts("The Monolithium Kernel 0.1 - By Alexander Andrejevic");
     for (i = 0; i < 80; i++) putchar('-');
 
-    memory_init((multiboot_mmap_t*)mboot->mmap_addr, mboot->mmap_length);
+    if (!mmap) KERNEL_CRASH("The bootloader failed to supply a memory map");
+    memory_init(mmap);
     heap_init();
 
     map_memory_internal((void*)TEXT_VIDEO_MEMORY,
@@ -102,8 +121,6 @@ void kernel_main(multiboot_header_t *mboot, dword_t stack)
                         TEXT_HEIGHT * TEXT_WIDTH * sizeof(word_t),
                         PAGE_PRESENT | PAGE_WRITABLE);
 
-    ASSERT(read_physical(cmdline_phys, cmdline, MAX_CMDLINE_LENGTH - 1) == ERR_SUCCESS);
-
     printf("%-70s", "Initializing devices...");
     device_init();
     set_text_color(TEXT_COLOR_GREEN | TEXT_COLOR_STRONG);
@@ -135,7 +152,7 @@ void kernel_main(multiboot_header_t *mboot, dword_t stack)
     set_text_color(TEXT_COLOR_RED | TEXT_COLOR_GREEN | TEXT_COLOR_BLUE);
 
     printf("%-70s", "Initializing processes...");
-    process_init("");
+    process_init();
     set_text_color(TEXT_COLOR_GREEN | TEXT_COLOR_STRONG);
     puts("done");
     set_text_color(TEXT_COLOR_RED | TEXT_COLOR_GREEN | TEXT_COLOR_BLUE);