From 5ddf0173c28c741859a06b5b0f2932bf40e73811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Tue, 14 Nov 2017 16:26:11 +0100 Subject: [PATCH] Remove magic numbers. Also, no code segment - not needed. --- src/kernel/boot/boot.S | 16 +++++++++------ src/kernel/boot/boot_GDT.S | 14 +++++++++++++ src/kernel/boot/boot_PT.S | 22 ++++++++++++++++++++ src/kernel/boot/boot_pagedir.S | 33 ------------------------------ src/kernel/include/x86_64/gdt.h | 6 ++++++ src/kernel/include/x86_64/memory.h | 8 ++++++++ 6 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 src/kernel/boot/boot_GDT.S create mode 100644 src/kernel/boot/boot_PT.S delete mode 100644 src/kernel/boot/boot_pagedir.S create mode 100644 src/kernel/include/x86_64/gdt.h create mode 100644 src/kernel/include/x86_64/memory.h diff --git a/src/kernel/boot/boot.S b/src/kernel/boot/boot.S index 4be4593..d451f96 100644 --- a/src/kernel/boot/boot.S +++ b/src/kernel/boot/boot.S @@ -1,8 +1,9 @@ +#include .intel_syntax noprefix .section .bss -.align 0x1000 -.skip 0x1000 +.align PAGE_SIZE +.skip PAGE_SIZE BootStack: .byte 0 @@ -71,10 +72,6 @@ mov cr0, eax lgdt [BootGDTp] -mov eax, 0x10 -mov ss, eax -mov ds, eax -mov es, eax .extern long_mode_start jmp 0x8:long_mode_start @@ -82,4 +79,11 @@ mov es, eax .code64 long_mode_start: +mov eax, 0x0 +mov ss, eax +mov ds, eax +mov es, eax +mov fs, eax +mov gs, eax + jmp $ diff --git a/src/kernel/boot/boot_GDT.S b/src/kernel/boot/boot_GDT.S new file mode 100644 index 0000000..3406c8b --- /dev/null +++ b/src/kernel/boot/boot_GDT.S @@ -0,0 +1,14 @@ +#include +#include +.intel_syntax noprefix + +.section .rodata +.align PAGE_SIZE +.global BootGDT +.global BootGDTp +BootGDT: + .long 0, 0 + .long 0, (GDT_PRESENT | GDT_DPL(0) | GDT_CODE | GDT_LONG) +BootGDTp: + .short 2*8-1 + .quad offset BootGDT diff --git a/src/kernel/boot/boot_PT.S b/src/kernel/boot/boot_PT.S new file mode 100644 index 0000000..a4fd20f --- /dev/null +++ b/src/kernel/boot/boot_PT.S @@ -0,0 +1,22 @@ +#include +.intel_syntax noprefix + +.section .data +.align PAGE_SIZE +.global BootP4 +BootP4: + .quad offset BootP3 + (PAGE_PRESENT | PAGE_WRITE) + .rept ENTRIES_PER_PT - 1 + .quad 0x0 + .endr +BootP3: + .quad offset BootP2 + (PAGE_PRESENT | PAGE_WRITE) + .rept ENTRIES_PER_PT - 1 + .quad 0x0 + .endr +BootP2: + .set i, 0 + .rept ENTRIES_PER_PT + .quad (i << 21 ) + (PAGE_PRESENT | PAGE_WRITE | PAGE_HUGE) + .set i, (i+1) + .endr diff --git a/src/kernel/boot/boot_pagedir.S b/src/kernel/boot/boot_pagedir.S deleted file mode 100644 index 6284ac5..0000000 --- a/src/kernel/boot/boot_pagedir.S +++ /dev/null @@ -1,33 +0,0 @@ -.intel_syntax noprefix - -.section .data -.align 0x1000 -.global BootP4 -BootP4: - .quad offset BootP3 + 0x3 - .rept 511 - .quad 0x0 - .endr -BootP3: - .quad offset BootP2 + 0x3 - .rept 511 - .quad 0x0 - .endr -BootP2: - .set i, 0 - .rept 512 - .quad (i << 21 ) + 0x83 - .set i, (i+1) - .endr - -.section .rodata -.align 0x1000 -.global BootGDT -.global BootGDTp -BootGDT: - .quad 0 - .quad ((1<<47) + (1<<44) + (1<<41) + (1<<43) + (1<<53)) - .quad ((1<<47) + (1<<44) + (1<<41)) -BootGDTp: - .short 3*8-1 - .quad offset BootGDT diff --git a/src/kernel/include/x86_64/gdt.h b/src/kernel/include/x86_64/gdt.h new file mode 100644 index 0000000..73b958c --- /dev/null +++ b/src/kernel/include/x86_64/gdt.h @@ -0,0 +1,6 @@ +#pragma once + +#define GDT_CODE (3<<11) +#define GDT_DPL(lvl) ((lvl)<<13) +#define GDT_PRESENT (1<<15) +#define GDT_LONG (1<<21) diff --git a/src/kernel/include/x86_64/memory.h b/src/kernel/include/x86_64/memory.h new file mode 100644 index 0000000..31b09e8 --- /dev/null +++ b/src/kernel/include/x86_64/memory.h @@ -0,0 +1,8 @@ +#pragma once + +#define PAGE_PRESENT 0x001 +#define PAGE_WRITE 0x002 +#define PAGE_HUGE 0x080 + +#define PAGE_SIZE 0x1000 +#define ENTRIES_PER_PT 512