From b96b09a8b562434f9f74dc5879c22a6d9cddc4fd Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 13 Oct 2022 06:43:12 +0300 Subject: [PATCH] kernel boots with multiboot2 --- Architecture/aarch64/boot.S | 2 +- Architecture/aarch64/linker.ld | 4 +- Architecture/amd64/linker.ld | 6 +++ Architecture/i686/boot.asm | 72 ++++++++++++++++++++++++++++++++++ Architecture/i686/linker.ld | 45 +++++++++++++-------- 5 files changed, 109 insertions(+), 20 deletions(-) create mode 100644 Architecture/i686/boot.asm diff --git a/Architecture/aarch64/boot.S b/Architecture/aarch64/boot.S index 5d524b2..cf7502c 100644 --- a/Architecture/aarch64/boot.S +++ b/Architecture/aarch64/boot.S @@ -11,7 +11,7 @@ _start: str xzr, [x5], #8 sub w6, w6, #1 cbnz w6, 1b -2: bl aarch64Entry +2: bl arm64Entry Halt: wfe b Halt diff --git a/Architecture/aarch64/linker.ld b/Architecture/aarch64/linker.ld index c6567c6..12f5c58 100644 --- a/Architecture/aarch64/linker.ld +++ b/Architecture/aarch64/linker.ld @@ -36,7 +36,7 @@ SECTIONS /DISCARD/ : { - *(.eh_frame) - *(.note .note.*) + *(.comment*) + *(.note*) } } diff --git a/Architecture/amd64/linker.ld b/Architecture/amd64/linker.ld index 07992d3..81d377d 100644 --- a/Architecture/amd64/linker.ld +++ b/Architecture/amd64/linker.ld @@ -36,4 +36,10 @@ SECTIONS } . += CONSTANT(MAXPAGESIZE); _kernel_end = ALIGN(CONSTANT(MAXPAGESIZE)); + + /DISCARD/ : + { + *(.comment*) + *(.note*) + } } diff --git a/Architecture/i686/boot.asm b/Architecture/i686/boot.asm new file mode 100644 index 0000000..d371c46 --- /dev/null +++ b/Architecture/i686/boot.asm @@ -0,0 +1,72 @@ +KERNEL_VIRTUAL_BASE EQU 0xE0000000 + +section .multiboot2.data +align 8 +HEADER_START: + dd 0xE85250D6 + dd 0 + dd (HEADER_END - HEADER_START) + dd -(0xE85250D6 + (HEADER_END - HEADER_START)) + +align 8 + dw 0 + dw 0 + dd 8 + +HEADER_END: + +section .bss +STACK_BOTTOM: resb 16384 +STACK_TOP: + +align 4096 +BOOT_PAGE_DIR0: resb 4096 +BOOT_PAGE_TBL0: resb 4096 +BOOT_PAGE_TBL1: resb 4096 + +section .multiboot2.text +extern x32Entry +extern _kernel_start +extern _kernel_end +global _start + +_start: + mov esp, STACK_TOP - KERNEL_VIRTUAL_BASE + mov edi, BOOT_PAGE_TBL0 - KERNEL_VIRTUAL_BASE + mov esi, 0 + mov ecx, 2048 - 301 +.PagingLoop: + cmp esi, _kernel_start - KERNEL_VIRTUAL_BASE + jl .LoopInside + cmp esi, _kernel_end - KERNEL_VIRTUAL_BASE + jge .LoopEnd + mov eax, esi + or eax, 3 + mov [edi], eax +.LoopInside: + add esi, 4096 + add edi, 4 + loop .PagingLoop +.LoopEnd: + push ebx + mov dword [BOOT_PAGE_DIR0 - KERNEL_VIRTUAL_BASE + (000 * 4)], (BOOT_PAGE_TBL0 - KERNEL_VIRTUAL_BASE + 3) + mov dword [BOOT_PAGE_DIR0 - KERNEL_VIRTUAL_BASE + (001 * 4)], (BOOT_PAGE_TBL1 - KERNEL_VIRTUAL_BASE + 3) + mov dword [BOOT_PAGE_DIR0 - KERNEL_VIRTUAL_BASE + (896 * 4)], (BOOT_PAGE_TBL0 - KERNEL_VIRTUAL_BASE + 3) + mov dword [BOOT_PAGE_DIR0 - KERNEL_VIRTUAL_BASE + (897 * 4)], (BOOT_PAGE_TBL1 - KERNEL_VIRTUAL_BASE + 3) + mov ecx, BOOT_PAGE_DIR0 - KERNEL_VIRTUAL_BASE + mov cr3, ecx + mov ecx, cr0 + or ecx, 0x80010000 + mov cr0, ecx + add esp, KERNEL_VIRTUAL_BASE + mov eax, CallKernelMain + jmp eax + +section .text +CallKernelMain: + push ebx + call x32Entry + cli +.hang: + hlt + jmp .hang diff --git a/Architecture/i686/linker.ld b/Architecture/i686/linker.ld index aad40cb..0a76813 100644 --- a/Architecture/i686/linker.ld +++ b/Architecture/i686/linker.ld @@ -1,45 +1,56 @@ OUTPUT_FORMAT(elf32-i386) OUTPUT_ARCH(i386) -ENTRY(Entry) +ENTRY(_start) + +KERNEL_VIRTUAL_BASE = 0xE0000000; SECTIONS { - . += 0xC0000000; - _kernel_start = .; + . = 0x00100000; + _kernel_start = . + KERNEL_VIRTUAL_BASE; - .text : + .multiboot.data : + { + *(.multiboot2.data) + } + + .multiboot.text : + { + *(.multiboot2.text) + } + + . += KERNEL_VIRTUAL_BASE; + .text ALIGN(4096) : AT(ADDR(.text) - KERNEL_VIRTUAL_BASE) { *(.text .text.*) } - _kernel_text_end = ALIGN(CONSTANT(MAXPAGESIZE)); - . += CONSTANT(MAXPAGESIZE); + _kernel_text_end = .; - .data : + .data ALIGN (4096) : AT(ADDR(.data) - KERNEL_VIRTUAL_BASE) { *(.data .data.*) } - _kernel_data_end = ALIGN(CONSTANT(MAXPAGESIZE)); - . += CONSTANT(MAXPAGESIZE); + _kernel_data_end = .; - .rodata : + .rodata ALIGN (4096) : AT(ADDR(.rodata) - KERNEL_VIRTUAL_BASE) { *(.rodata .rodata.*) } - _kernel_rodata_end = ALIGN(CONSTANT(MAXPAGESIZE)); - . += CONSTANT(MAXPAGESIZE); + _kernel_rodata_end = .; - .bss : + .bss ALIGN (4096) : AT(ADDR(.bss) - KERNEL_VIRTUAL_BASE) { + _sbss = .; *(COMMON) *(.bss .bss.*) + _ebss = .; } - . += CONSTANT(MAXPAGESIZE); - _kernel_end = ALIGN(CONSTANT(MAXPAGESIZE)); + _kernel_end = .; /DISCARD/ : { - *(.eh_frame) - *(.note .note.*) + *(.comment*) + *(.note*) } }