From fddedb44488ae552e3dfa0b30cd1e800ee187a6b Mon Sep 17 00:00:00 2001 From: Morten Delenk Date: Thu, 16 Jun 2016 20:50:03 +0200 Subject: [PATCH] First step towards x86_64 --- kernel.settings | 11 ++- kernel/hal/x86_64/boot/boot.S | 127 ++++++++++++++++++++++++++++ kernel/{hal/x86 => kernel}/string.c | 0 test.elf | Bin 18560 -> 18616 bytes 4 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 kernel/hal/x86_64/boot/boot.S rename kernel/{hal/x86 => kernel}/string.c (100%) diff --git a/kernel.settings b/kernel.settings index fe4af82..078b7d3 100644 --- a/kernel.settings +++ b/kernel.settings @@ -1,10 +1,15 @@ -arch = x86 +arch = x86_64 #MODE = debug MODE = release # enables optimization export PATH := $(HOME)/opt/bin:$(PATH) -#PREFIX = -PREFIX = i686-elf- +ifeq ($(arch),x86) + PREFIX = i686-elf- +else + ifeq ($(arch),x86_64) + PREFIX = x86_64-elf- + endif +endif ifeq ($(MODE),debug) CFLAGS += -g3 -DDEBUG diff --git a/kernel/hal/x86_64/boot/boot.S b/kernel/hal/x86_64/boot/boot.S new file mode 100644 index 0000000..f1046b7 --- /dev/null +++ b/kernel/hal/x86_64/boot/boot.S @@ -0,0 +1,127 @@ +.code32 +.section multiboot +#define MB_MAGIC 0x1BADB002 +#define MB_FLAGS 0x0 +#define MB_CHECKSUM -(MB_MAGIC + MB_FLAGS) +.align 4 +.int MB_MAGIC +.int MB_FLAGS +.int MB_CHECKSUM +.section .text +.extern init +.global _start +_start: + mov $mb_ptr, %edi + stosl + mov %ebx, %eax + stosl //Temponarily store both multiboot header pointer and multiboot + mov $kernel_stack, %esp + mov $0x80000001, %eax //Check if Long Mode is supported + cpuid + and $0x20000000, %edx + jz x86_64_err + + xor %eax, %eax //Check if PAE is supported + inc %eax + cpuid + and $0x40, %dl + jz x86_64_err + jmp x86_64_OK +x86_64_err: + cli + hlt + jmp x86_64_err +x86_64_OK: + //Fill one entry of both the pagemap L4 and the Pagedir PT + mov $pmfill, %esi + mov $pagemapL4, %edi + lodsl + stosl + lodsl + stosl + mov $pagedirPT, %edi + lodsl + stosl + lodsl + stosl + //Activate PAE + mov %cr4, %eax + or $0x20, %al + mov %eax, %cr4 + //Activate x86_64 + mov $0xC0000080, %ecx + rdmsr + or $0x00000100, %eax + wrmsr + //Load Page Table + mov $pagemapL4, %eax + mov %eax, %cr3 + //Activate Paging + mov %cr0, %eax + bswap %eax + or $0x80, %al + bswap %eax + mov %eax, %cr0 + //Jump to x86_64loader + lgdt gdtr + jmp $0x18, $start + +.code64 +//extern "C" void init(uint32_t eax, multiboot_info * ebx, uint64_t **** pt); +.global init +start: + xor %rax, %rax + mov $mb_ptr, %rsi + lodsl + mov %rax, %rdi + lodsl + mov %rax, %rsi + mov $pagemapL4, %rdx + call init +stop: + cli + hlt + jmp stop +.section .data +gdt: +//NULL-descriptor +.quad 0 +//32-bit code +.word 0xFFFF +.word 0x0000 +.byte 0x00 +.byte 0x98 +.byte 0xCF +.byte 0x00 +//data +.word 0xFFFF +.word 0x0000 +.byte 0x00 +.byte 0x92 +.byte 0xCF +.byte 00 +//64-bit code +.int 0 +.byte 0 +.byte 0x98 +.byte 0x20 +.byte 0 + +gdtr: + .word 4 * 8 + .int gdt +pmfill: +.int pagedirPT + 0x7 +.int 0 +pdptfill: +.quad 0x87 +.section .bss +mb_ptr: +// These 8 Bytes will never be used when the stack comes near it +.space 4096 +kernel_stack: +.align 4096 +pagemapL4: +.space 4096 +pagedirPT: +.space 4096 diff --git a/kernel/hal/x86/string.c b/kernel/kernel/string.c similarity index 100% rename from kernel/hal/x86/string.c rename to kernel/kernel/string.c diff --git a/test.elf b/test.elf index 0d5a60f10e8d2a331a7260e2912665b52a3ecbe0..c31f7970fd4a185301214c0415aea2e6f0c6cf17 100755 GIT binary patch delta 778 zcmZ8e&ubG=5T4m=YD+>%@|t#cn_p{bV?iyeHEL35ZPhe4JqQ*&Xo+bL#UF`91SNW~ z2M^Ntz*9imn@~gvf*uPZd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