summaryrefslogtreecommitdiff
path: root/src/bootloader
diff options
context:
space:
mode:
authorBrett Weiland <brett_weiland@bpcspace.com>2021-03-24 15:36:54 -0500
committerBrett Weiland <brett_weiland@bpcspace.com>2021-03-24 15:38:09 -0500
commit14b109ea24dc5cb1db948de57a2a44c80ef4622e (patch)
treee397997d9c8d9c1b50a5a5235d38c6b0e8f24b5b /src/bootloader
parent13000d6f44d66da795e92e4b2ddc37ce879bf275 (diff)
modified: README.md
modified: compiler/create_crosscompiler.sh deleted: notes modified: src/.gdb_history deleted: src/amd64_vol2.pdf modified: src/bootloader/bios_functions/bios_disk.asm modified: src/bootloader/bios_functions/print.asm modified: src/bootloader/bootloader.asm modified: src/bootloader/cpu_check.asm modified: src/bootloader/enter_kernel.asm deleted: src/bootloader/enter_kernel_backup modified: src/bootloader/gdt.asm deleted: src/bootloader/multicore.asm deleted: src/bootloader/notes new file: src/debug/gdbinit.gdb deleted: src/indigo_os modified: src/kernel/include/libc.h modified: src/kernel/include/paging.h modified: src/kernel/include/video.h modified: src/kernel/kernel.c modified: src/kernel/libs/acpi.c modified: src/kernel/libs/drivers/serial.c modified: src/kernel/libs/drivers/video.c modified: src/kernel/libs/libc.c modified: src/kernel/libs/page.c modified: src/link.ld modified: src/makefile new file: tools/README.md modified: tools/page/page.py
Diffstat (limited to 'src/bootloader')
-rw-r--r--src/bootloader/bios_functions/bios_disk.asm2
-rw-r--r--src/bootloader/bios_functions/print.asm29
-rw-r--r--src/bootloader/bootloader.asm10
-rw-r--r--src/bootloader/cpu_check.asm51
-rw-r--r--src/bootloader/enter_kernel.asm13
-rw-r--r--src/bootloader/enter_kernel_backup87
-rw-r--r--src/bootloader/gdt.asm2
-rw-r--r--src/bootloader/multicore.asm10
-rw-r--r--src/bootloader/notes0
9 files changed, 59 insertions, 145 deletions
diff --git a/src/bootloader/bios_functions/bios_disk.asm b/src/bootloader/bios_functions/bios_disk.asm
index 28fc74d..7957814 100644
--- a/src/bootloader/bios_functions/bios_disk.asm
+++ b/src/bootloader/bios_functions/bios_disk.asm
@@ -3,6 +3,8 @@ bios_disk:
pusha
mov es, dx
+; TODO when our kernel gets to big to fit in one track, we need to just switch over to LHS with extended bios functions.
+
mov ah, 0x02 ; read disc sectors
mov ch, 0x00 ; track 0
mov dh, 0x00 ; head 0
diff --git a/src/bootloader/bios_functions/print.asm b/src/bootloader/bios_functions/print.asm
index d071cd9..303fa8f 100644
--- a/src/bootloader/bios_functions/print.asm
+++ b/src/bootloader/bios_functions/print.asm
@@ -1,25 +1,24 @@
-;TODO fix null problem, allow direct passes
+;TODO fix null problem, allow passing value insted of pointer
bios_print:
-;push ax ; we need to use ax, so let's push it in case whoever called needs it
pusha
-mov ah, 0x0e ; tells bios we're in tty mode
+mov ah, 0x0e
.print_loop:
-mov al, [bx] ; al is the char MEANT TO BE CX
-cmp al, 0 ; compare the char we're about to print with null
-je .fini ; if it is null, we gonna scoot the fuck outta here
+mov al, [bx]
+cmp al, 0
+je .fini
test cx, cx ; if cx is zero, ascii, otherwise hex string
jne .print_hex
-int 0x10 ; bios call, actually prints the char
+int 0x10
.print_hex_ret:
-inc bx ; adds to the pointer
-jmp .print_loop ; goes back to loop start
+inc bx
+jmp .print_loop
.fini:
-mov al, 0xd ; \r
+mov al, 0xd
int 0x10
-mov al, 0xa ; \n
+mov al, 0xa
int 0x10
popa
ret
@@ -32,7 +31,7 @@ int 0x10
mov al, 'x'
int 0x10
-mov al, [bx] ; shift bits to get first nibble
+mov al, [bx]
shr al, 4
call .bios_print_nibble
@@ -47,13 +46,13 @@ jmp .print_hex_ret
.bios_print_nibble:
-cmp al, 9 ; see if letter worthy
+cmp al, 9
ja .print_hex_letter
-add al, 0x30 ;'1'
+add al, 0x30
int 0x10
ret
.print_hex_letter:
-add al, 0x57 ;'a'
+add al, 0x57
int 0x10
ret
diff --git a/src/bootloader/bootloader.asm b/src/bootloader/bootloader.asm
index 14116db..947965f 100644
--- a/src/bootloader/bootloader.asm
+++ b/src/bootloader/bootloader.asm
@@ -3,7 +3,6 @@
[extern _bootloader_stage1_size]
[extern _kernel_loc]
-;TODO clean up unreal mode
jmp stage0
times 3-($-$$) db 0x90 ; a temporary dirty fix to emulate a floppy disk insted of a hard risk
times 59 db 0 ; (TODO support hard disks)
@@ -59,7 +58,6 @@ dw 0xaa55
%include "bootloader/gdt.asm"
boot_msg:
-.debugmsg: db "debugeroni", 0
.kernel_loaded: db `Kernel loaded!\r\nBooting to protected, then long mode...`, 0
.stage2_loaded: db `Done loading bootloader!\r\nLoading kernel...`, 0
@@ -94,9 +92,10 @@ mov cr0, eax
pop ds
sti
+
;we are now in unreal mode
-mov cl, 5 ; starting sector TODO automate this
+mov cl, 5
mov edi, _kernel_loc
.loop:
mov al, 0x1 ; sector count
@@ -114,10 +113,6 @@ mov ecx, 512
a32 rep movsb
nop
-mov bx, boot_msg.debugmsg
-mov cx, 0
-call bios_print
-
pop cx
cmp cl, _kernel_size
@@ -142,4 +137,5 @@ jmp $
%include "bootloader/cpu_check.asm"
%include "bootloader/video.asm"
%include "bootloader/enter_kernel.asm"
+
times 2048 - ($ - $$) db 0
diff --git a/src/bootloader/cpu_check.asm b/src/bootloader/cpu_check.asm
index ffdd908..0fa6ddf 100644
--- a/src/bootloader/cpu_check.asm
+++ b/src/bootloader/cpu_check.asm
@@ -1,38 +1,41 @@
-; TODO move this file to something like "system_check", now that we do ram checks here
[extern _meminfo_loc]
detect_arch:
-; todo: figure out how to just handle invalid opcode exceptions, it would be faster and (maybe?) easier
-; todo: digest this part a little better when it's _not_ 1am.
-pushfd ; pushes eflags to stack
-pushfd ; pushes eflags to stack a second time
-xor dword [esp], 0x00200000 ; inverses the ID bit to see if cpuid is a thing
-pop eax ;gets our modified cpuid back
-xor eax, [esp] ; checks with the first time we pushed it; the OG eflag
-popfd ; pops the flags back I guess?
-and eax, 0x00200000 ; checks to see if the bit was inverted
+
+; detect CPUID
+
+pushfd
+pushfd
+xor dword [esp], 0x00200000
+pop eax
+xor eax, [esp]
+popfd
+and eax, 0x00200000
jz .print_cpuid_error
-;now we detect if we support long slong (aka 64 bit)
-;https://www.amd.com/system/files/TechDocs/25481.pdf page 21
-;So apperently some dumbass engineers thought it would be funny to sell CPUs that have the long mode bit set without it being supported, dispite including it in their own documentation.
-;there's a work around by checking other bits, and it's a low priority TODO. However, I really think us coming accross this will be extremely rare.
+; detect long mode
+
mov eax, 0x80000001
-cpuid ;cpuid returns into edx
-and edx, 0x0020000000 ; the 21st bit is cpuid.
+cpuid
+and edx, 0x0020000000
jz .print_long_error
+; APIC
mov eax, 0x00000001
cpuid
-and edx, 0x00000200
+test edx, 0x00000200
jz .print_apic_error
+; SSE (you might be able to remove this, I think it's a standard requirement)
+test edx, 0x2000000
+jz .print_sse_error
+
mov bx, .no_error
mov cx, 0
call bios_print
-;mapping memory
+; get memory mappings
mov ax, 0
mov es, ax
mov ax, _meminfo_loc
@@ -71,7 +74,7 @@ call bios_print
jmp $
.print_long_error:
-mov bx, .arch_error ; lets just test for now
+mov bx, .arch_error
mov cx, 0
call bios_print
jmp $
@@ -80,11 +83,19 @@ jmp $
mov bx, .apic_error
mov cx, 0
call bios_print
+jmp $
.print_mem_error:
mov bx, .mem_error
mov cx, 0
call bios_print
+jmp $
+
+.print_sse_error:
+mov bx, .sse_error
+mov cx, 0
+call bios_print
+jmp $
.cpuid_error:
@@ -95,6 +106,8 @@ call bios_print
db "No apic support", 0x0
.mem_error:
db "Could not get information on memory!", 0x0
+.sse_error:
+ db "This OS requires SSE", 0x0
.no_error:
db "CPU info gathered!", 0x0
diff --git a/src/bootloader/enter_kernel.asm b/src/bootloader/enter_kernel.asm
index c9991e3..d3933cf 100644
--- a/src/bootloader/enter_kernel.asm
+++ b/src/bootloader/enter_kernel.asm
@@ -1,5 +1,6 @@
[extern main]
[extern _kernel_stack_loc]
+[extern _stage1_pagetable]
enter_longmode:
cli
@@ -29,7 +30,7 @@ mov es, ax
mov fs, ax
mov gs, ax
-mov edi, 0x4000 ; 0x3000
+mov edi, _stage1_pagetable ; 0x3000
mov cr3, edi
mov eax, 0
mov ecx, 0xc00 ; 0x1000
@@ -37,14 +38,14 @@ rep stosd
mov edi, cr3
-mov DWORD [edi], 0x5003 ; pml4e[0] = pdpe
+mov DWORD [edi], _stage1_pagetable + 0x1003 ; pml4e[0] = pdpe
add edi, 0x1000
-mov DWORD [edi], 0x6003 ; pdpe[0] = pde
+mov DWORD [edi], _stage1_pagetable + 0x2003 ; pdpe[0] = pde
add edi, 0x1000
mov DWORD [edi], 0x83 ; pde[0] = pte
mov eax, cr4
-or eax, 1 << 5
+or eax, 0x620
mov cr4, eax
;end of setting up pages
@@ -65,9 +66,11 @@ or eax, 1 << 8
wrmsr
mov eax, cr0
-or eax, 1 << 31
+and ax, 0xfffb
+or eax, 0x80000002
mov cr0, eax
+
lgdt [long_gdt.descriptor]
jmp LONG_CODE_SEGMENT:enter_kernel
diff --git a/src/bootloader/enter_kernel_backup b/src/bootloader/enter_kernel_backup
deleted file mode 100644
index 7631f20..0000000
--- a/src/bootloader/enter_kernel_backup
+++ /dev/null
@@ -1,87 +0,0 @@
-[extern main]
-[extern _kernel_stack_loc]
-enter_longmode:
-cli
-
-; TODO check if a20 is already set
-mov al, 0x92
-or al, 2
-out 0x92, al
-
-lgdt [protected_gdt.descriptor]
-mov eax, cr0
-or eax, 0x1
-mov cr0, eax
-
-mov eax, 0x8
-mov ds, eax
-
-jmp 0x8:init_longmode
-
-bits 32
-init_longmode:
-mov ebp, 0xffff
-mov esp, ebp
-mov ax, PROTECTED_DATA_SEGMENT
-mov ds, ax
-mov ss, ax
-mov es, ax
-mov fs, ax
-mov gs, ax
-
-mov edi, 0x3000 ; this is where our page tables will be
-mov cr3, edi
-mov eax, 0 ; what we'll be putting there
-mov ecx, 4096 ; how many times we'll put it there
-rep stosd ; kinda like memset(&edi, ecx, edi)
-mov edi, cr3
-
-mov DWORD [edi], 0x4003 ; pml4e[0] = pdpe
-add edi, 0x1000
-mov DWORD [edi], 0x5003 ; pdpe[0] = pde
-add edi, 0x1000
-mov DWORD [edi], 0x6003 ; pde[0] = pte
-add edi, 0x1000
-
-mov ebx, 0x00000003 ; the flags
-mov ecx, 512; the loop counter, will map 2 mib
-
-.idmap_pte_loop:
-mov DWORD [edi], ebx
-add ebx, 0x1000 ; physical address. Should leave us at 0x7000
-add edi, 8 ; position in page table
-loop .idmap_pte_loop
-
-mov eax, cr4
-or eax, 1 << 5
-mov cr4, eax
-
-mov ecx, 0xc0000080
-rdmsr
-or eax, 1 << 8
-wrmsr
-
-mov eax, cr0
-or eax, 1 << 31 | 1 << 0 ; this is where we set paging and protected mode (respectively)!
-mov cr0, eax
-
-
-mov ecx, 0xc0000080
-rdmsr
-or eax, 1 << 8
-wrmsr
-
-mov eax, cr0
-or eax, 1 << 31
-mov cr0, eax
-
-lgdt [long_gdt.descriptor]
-
-jmp LONG_CODE_SEGMENT:enter_kernel
-enter_kernel:
-bits 64
-mov rbp, _kernel_stack_loc
-mov rsp, _kernel_stack_loc
-call main ; where we actually call the kernel
-jmp $
-ret
diff --git a/src/bootloader/gdt.asm b/src/bootloader/gdt.asm
index d506cdf..0ba0a44 100644
--- a/src/bootloader/gdt.asm
+++ b/src/bootloader/gdt.asm
@@ -49,8 +49,6 @@ db 10011010b ;1st flags and type. The first four bits (1010) are type, and the
db 10101111b ;1111 is segment limit continued. 0: available, 0: 64 bit (change?), 1: 32 bit segment, 1: granularity (shifts 3 hex didgets to get all of memory)
db 0
-; the data GDT is the exact same, and it overlaps. We don't care, we'll be booting into long mode right after then programming in C
-; TODO: clarify once you figure out what you're doing
.gdt_data:
dw 0
dw 0
diff --git a/src/bootloader/multicore.asm b/src/bootloader/multicore.asm
deleted file mode 100644
index fa33ea5..0000000
--- a/src/bootloader/multicore.asm
+++ /dev/null
@@ -1,10 +0,0 @@
-multicore_boot:
-jmp $
-
-mov ecx, 0x0000001b
-rdmsr
-;mov [apic_register] dx:ax
-
-apic_register: dq 0
-multicore_msg1: db "CPUs available: ", 0
-
diff --git a/src/bootloader/notes b/src/bootloader/notes
deleted file mode 100644
index e69de29..0000000
--- a/src/bootloader/notes
+++ /dev/null