summaryrefslogtreecommitdiff
path: root/src/arch/amd64/boot.S
diff options
context:
space:
mode:
Diffstat (limited to 'src/arch/amd64/boot.S')
-rw-r--r--src/arch/amd64/boot.S112
1 files changed, 82 insertions, 30 deletions
diff --git a/src/arch/amd64/boot.S b/src/arch/amd64/boot.S
index fc580fb..9771fe2 100644
--- a/src/arch/amd64/boot.S
+++ b/src/arch/amd64/boot.S
@@ -3,18 +3,6 @@ extern kmain
extern amd64_shim
bits 32
-; base, limit, access, flags
-%macro gdt_entry 4
- db %2 & 0xff
- db (%2 >> 8) & 0xff
- db %1 & 0xff
- db (%1 >> 8) & 0xff
- db (%1 >> 16) & 0xff
- db %3
- db ((%2 >> 16) & 0x0f) | (%4 << 4)
- db (%1 >> 24) & 0xff
-%endmacro
-
section .multiboot
align 8
mb_start:
@@ -38,33 +26,97 @@ stack_end:
section .rodata
align 16
-gdt_start:
-gdt_entry 0, 0, 0, 0
-gdt_entry 0, 0xFFFFF, 0x9A, 0xC
-gdt_entry 0, 0xFFFFF, 0x92, 0xC
-gdt_end:
-gdt_descriptor:
- dw gdt_end - gdt_start - 1
- dd gdt_start
-
+; Access bits
+PRESENT equ 1 << 7
+NOT_SYS equ 1 << 4
+EXEC equ 1 << 3
+DC equ 1 << 2
+RW equ 1 << 1
+ACCESSED equ 1 << 0
+
+; Flags bits
+GRAN_4K equ 1 << 7
+SZ_32 equ 1 << 6
+LONG_MODE equ 1 << 5
+
+GDT:
+ .Null: equ $ - GDT
+ dq 0
+ .Code: equ $ - GDT
+ dd 0xFFFF ; Limit & Base (low, bits 0-15)
+ db 0 ; Base (mid, bits 16-23)
+ db PRESENT | NOT_SYS | EXEC | RW ; Access
+ db GRAN_4K | LONG_MODE | 0xF ; Flags & Limit (high, bits 16-19)
+ db 0 ; Base (high, bits 24-31)
+ .Data: equ $ - GDT
+ dd 0xFFFF ; Limit & Base (low, bits 0-15)
+ db 0 ; Base (mid, bits 16-23)
+ db PRESENT | NOT_SYS | RW ; Access
+ db GRAN_4K | SZ_32 | 0xF ; Flags & Limit (high, bits 16-19)
+ db 0 ; Base (high, bits 24-31)
+ .TSS: equ $ - GDT
+ dd 0x00000068
+ dd 0x00CF8900
+ .Pointer:
+ dw $ - GDT - 1
+ dq GDT
section .text
align 8
start:
cli
- lgdt [gdt_descriptor]
- jmp 0x08:after_lgdt
-after_lgdt:
- mov ax, 0x10
- mov ds, ax
- mov ss, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
mov esp, stack_end
mov ebp, stack_end
+
push ebx
+
+ mov edi, 0x1000
+ mov cr3, edi
+ xor eax, eax
+ mov ecx, 4096
+ rep stosd
+ mov edi, cr3
+
+ mov DWORD [edi], 0x2003 ; Set the uint32_t at the destination index to 0x2003.
+ add edi, 0x1000 ; Add 0x1000 to the destination index.
+ mov DWORD [edi], 0x3003 ; Set the uint32_t at the destination index to 0x3003.
+ add edi, 0x1000 ; Add 0x1000 to the destination index.
+ mov DWORD [edi], 0x4003 ; Set the uint32_t at the destination index to 0x4003.
+ add edi, 0x1000 ; Add 0x1000 to the destination index.
+
+ mov ebx, 0x00000003 ; Set the B-register to 0x00000003.
+ mov ecx, 512 ; Set the C-register to 512.
+
+.SetEntry:
+ mov DWORD [edi], ebx ; Set the uint32_t at the destination index to the B-register.
+ add ebx, 0x1000 ; Add 0x1000 to the B-register.
+ add edi, 8 ; Add eight to the destination index.
+ loop .SetEntry ; Set the next entry.
+
+ ;push ebx ; Call our function to set up basic paging
+ ;call amd64_shim
+
+
+ mov eax, cr4 ; Enable the PAE bit
+ or eax, 1 << 5
+ mov cr4, eax
+
+ mov ecx, 0xC0000080 ; Enable long mode
+ rdmsr
+ or eax, 1 << 8
+ wrmsr
+
+ mov eax, cr0 ; Enable paging
+ or eax, 1 << 31
+ mov cr0, eax
+
+ lgdt [GDT.Pointer]
+ jmp GDT.Code:code64
+
+ bits 64
+code64:
call amd64_shim
+ push rax
call kmain
cli
halt: