From ed0f5cc05b8e4c9b6d2c4ab22a9556aa9be37a97 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 6 Aug 2021 22:26:57 +0200 Subject: [PATCH 1/7] GDT --- src/pm/gdt.asm | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/pm/gdt.asm diff --git a/src/pm/gdt.asm b/src/pm/gdt.asm new file mode 100644 index 0000000..cfe2a75 --- /dev/null +++ b/src/pm/gdt.asm @@ -0,0 +1,20 @@ +; 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) + dw 0x0 ; Base (bits 16-23) + db 10011010b ; Flags1 : Type + db 11001111b ; Flags2 : Limit (16-19) + db 0x0 ; Base (bits 24-31) From ff6203f4bd62f600ea97861a0bd05d62d536dd6b Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 6 Aug 2021 22:31:52 +0200 Subject: [PATCH 2/7] More GDT stuff --- src/pm/gdt.asm | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/pm/gdt.asm b/src/pm/gdt.asm index cfe2a75..8ceb80b 100644 --- a/src/pm/gdt.asm +++ b/src/pm/gdt.asm @@ -14,7 +14,24 @@ gdt_code: ; Code SD dw 0xffff ; Limit (bits 0-15) dw 0x0 ; Base (bits 0-15) - dw 0x0 ; Base (bits 16-23) + 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 ; From 8bd3137f803d4f0a5c433fc97ae517319632eb86 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 6 Aug 2021 22:39:42 +0200 Subject: [PATCH 3/7] gdt.asm --- src/pm/gdt.asm | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/pm/gdt.asm b/src/pm/gdt.asm index 8ceb80b..c092d3e 100644 --- a/src/pm/gdt.asm +++ b/src/pm/gdt.asm @@ -34,4 +34,10 @@ 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 ; + 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 From 5c9ae462178d1547cff11abcea13556d726e200d Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 6 Aug 2021 22:42:50 +0200 Subject: [PATCH 4/7] Minor typo fix --- src/pm.asm | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/pm.asm b/src/pm.asm index 822cd96..58f5ade 100644 --- a/src/pm.asm +++ b/src/pm.asm @@ -1,2 +1,6 @@ -%include "equ/vga.asm" +; Utils +%include "equ/vga.asm" %include "pm/vga/print.asm" + +; GDT & switching to PM +%include "pm/gdt.asm" ; GDT From 8325d90e9b7a8293e52874b64abea28879d0c39a Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 6 Aug 2021 22:55:57 +0200 Subject: [PATCH 5/7] Indent fix --- src/bootloader.asm | 10 +++++----- src/pm.asm | 9 ++++++++- src/pm/gdt.asm | 22 +++++++++++----------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/bootloader.asm b/src/bootloader.asm index e54643c..6c91be3 100644 --- a/src/bootloader.asm +++ b/src/bootloader.asm @@ -11,9 +11,9 @@ call println ; Read second sector (outside bootsector) - mov bx, 0x9000 ; LOAD LOCATION - mov dh, 2 ; SECTOR-COUNT - mov dl, [BOOT_DRIVE] ; DISK-INDEX + mov bx, 0x9000 ; LOAD LOCATION + mov dh, 2 ; SECTOR-COUNT + mov dl, [BOOT_DRIVE] ; DISK-INDEX call disk_read @@ -31,12 +31,12 @@ %include "equ/ascii.asm" %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 BOOT_DRIVE: db 0 +%include "pm.asm" + ; Bootsector times 510-($-$$) db 0 dw 0xaa55 ; magic BIOS numbers diff --git a/src/pm.asm b/src/pm.asm index 58f5ade..4a0a2f0 100644 --- a/src/pm.asm +++ b/src/pm.asm @@ -3,4 +3,11 @@ %include "pm/vga/print.asm" ; GDT & switching to PM -%include "pm/gdt.asm" ; GDT +%include "pm/gdt.asm" ; GDT defined here + +; Switching to PM +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 diff --git a/src/pm/gdt.asm b/src/pm/gdt.asm index c092d3e..4023658 100644 --- a/src/pm/gdt.asm +++ b/src/pm/gdt.asm @@ -12,30 +12,30 @@ gdt_code: ; Code SD ; 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) + 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) + db 0x0 ; Base (bits 24-31) -gdt_data: ; Data SD +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) + 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) + 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 + 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 From 39166e7863e32e4ef763d2a1b6fdcd6f616c1312 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 6 Aug 2021 23:05:28 +0200 Subject: [PATCH 6/7] Switching to PM --- src/pm.asm | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/pm.asm b/src/pm.asm index 4a0a2f0..b74f3a4 100644 --- a/src/pm.asm +++ b/src/pm.asm @@ -6,8 +6,29 @@ %include "pm/gdt.asm" ; GDT defined here ; Switching to PM -lgdt [gdt_descriptor] ; Tell the CPU about the GDT +[bits 16] +switch_to_pm: + 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 + +[bits 32] +; Init registers & stack when in PM +init_pm: + 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 -mov eax, cr0 ; Set first bit of the CR0 register -or eax, 0x1 ; to 1 -mov cr0, eax ; Update the control register From 54759d32a502a35d8542e78a3b99a291271ea5f0 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 6 Aug 2021 23:14:46 +0200 Subject: [PATCH 7/7] Entered PM --- src/bootloader.asm | 33 ++++++++++----------------------- src/pm.asm | 7 +++++-- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/bootloader.asm b/src/bootloader.asm index 6c91be3..9815979 100644 --- a/src/bootloader.asm +++ b/src/bootloader.asm @@ -6,36 +6,23 @@ 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 %include "equ/ascii.asm" %include "bios.asm" +%include "pm.asm" -welcome_string: db "e Operating-System (eOS)", ASCII_CARRIAGE_RETURN, ASCII_LINEBREAK, "Version 2021 0.0", ASCII_END -BOOT_DRIVE: db 0 +BEGIN_PM: + mov ebx, welcome_string + call vga_print -%include "pm.asm" + jmp $ + +welcome_string: db "e Operating-System (eOS): Version 2021 0.0", ASCII_END +BOOT_DRIVE: db 0 ; Bootsector times 510-($-$$) db 0 diff --git a/src/pm.asm b/src/pm.asm index b74f3a4..001aeec 100644 --- a/src/pm.asm +++ b/src/pm.asm @@ -7,7 +7,7 @@ ; Switching to PM [bits 16] -switch_to_pm: +pm_preinit: cli ; Switch interupts lgdt [gdt_descriptor] ; Tell the CPU about the GDT @@ -16,9 +16,12 @@ switch_to_pm: 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 -init_pm: +pm_init: mov ax, GDT_DATA_SEG ; Point the segment registers to GDT_DATA_SEG ; Segment registers mov ds, ax