diff --git a/src/bootloader.asm b/src/bootloader.asm index e54643c..9815979 100644 --- a/src/bootloader.asm +++ b/src/bootloader.asm @@ -6,26 +6,8 @@ mov bp, 0x8000 ; move it to 0x8000 mov sp, bp - ; Print the welcome string - mov bx, welcome_string - call println - - ; Read second sector (outside bootsector) - mov bx, 0x9000 ; LOAD LOCATION - mov dh, 2 ; SECTOR-COUNT - mov dl, [BOOT_DRIVE] ; DISK-INDEX - call disk_read - - - ; Print out whatever bloated data that was read - mov dx, [0x9000] - call print_hex - - mov bx, [ASCII_END] - call println - - mov dx, [0x9000 + 512] ; read from second sector too - call print_hex + ; Switching to PM + call pm_preinit jmp $ ; inf loop @@ -33,8 +15,13 @@ %include "bios.asm" %include "pm.asm" -; Data -welcome_string: db "e Operating-System (eOS)", ASCII_CARRIAGE_RETURN, ASCII_LINEBREAK, "Version 2021 0.0", ASCII_END +BEGIN_PM: + mov ebx, welcome_string + call vga_print + + jmp $ + +welcome_string: db "e Operating-System (eOS): Version 2021 0.0", ASCII_END BOOT_DRIVE: db 0 ; Bootsector diff --git a/src/pm.asm b/src/pm.asm index 822cd96..001aeec 100644 --- a/src/pm.asm +++ b/src/pm.asm @@ -1,2 +1,37 @@ -%include "equ/vga.asm" +; Utils +%include "equ/vga.asm" %include "pm/vga/print.asm" + +; GDT & switching to PM +%include "pm/gdt.asm" ; GDT defined here + +; Switching to PM +[bits 16] +pm_preinit: + cli ; Switch interupts + + lgdt [gdt_descriptor] ; Tell the CPU about the GDT + + mov eax, cr0 ; Set first bit of the CR0 register + or eax, 0x1 ; to 1 + mov cr0, eax ; Update the control register + + ; Initialize PM + jmp GDT_CODE_SEG:pm_init + +[bits 32] +; Init registers & stack when in PM +pm_init: + mov ax, GDT_DATA_SEG ; Point the segment registers to GDT_DATA_SEG + ; Segment registers + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + + mov ebp, 0x900000 ; Update the stack pointer + mov esp, ebp + + call BEGIN_PM + diff --git a/src/pm/gdt.asm b/src/pm/gdt.asm new file mode 100644 index 0000000..4023658 --- /dev/null +++ b/src/pm/gdt.asm @@ -0,0 +1,43 @@ +; GDT + +gdt_start: + +gdt_null: ; NULL descriptor + dd 0x0 ; 4-bytes of + dd 0x0 ; 0x0 (0) = NULL + +gdt_code: ; Code SD + ; Range: 0x0 - 0xfffff + ; Flags1: 1001b (present:2 privilege:descriptor type) + ; Type: 1010b (code:conforming:readable:accessed) + ; Flags2: 1100b (granularity:32bit default:64bit seg:AVL) + + dw 0xffff ; Limit (bits 0-15) + dw 0x0 ; Base (bits 0-15) + db 0x0 ; Base (bits 16-23) + db 10011010b ; Flags1 : Type + db 11001111b ; Flags2 : Limit (16-19) + db 0x0 ; Base (bits 24-31) + +gdt_data: ; Data SD + ; Same flags as gdt_code but different type flags + ; Type: 0010b (code:expand dowm:writable:accessed) + + dw 0xffff ; Limit (bits 0-15) + dw 0x0 ; Base (bits 0-15) + db 0x0 ; Base (bits 16-23) + db 10010010b ; Flags1 : Type + db 11001111b ; Flags2 : Limit (16-19) + db 0x0 ; Base (bits 24-31) + +gdt_end: ; Mark the end so we can get the delta-memory (size of GDT) + +; GDT descriptor +gdt_descriptor: + dw gdt_end - gdt_start - 1 ; Size of GDT is always -1 of actual size (WHY?!?!?!?!??!) + dd gdt_start ; Pointer to the GDT + + +; Lazy constants +GDT_CODE_SEG equ gdt_code - gdt_start +GDT_DATA_SEG equ gdt_data - gdt_start