Skip to content

Commit

Permalink
feat: setup paging and map kernel to higher half
Browse files Browse the repository at this point in the history
Kernel is now at 0xC0100000, but still we use 4MiB pages, instruction like 'invlpg' which are invalid for cpu prior to 486, and we don't ensure multiboot structures are mapped. Still lot of work
  • Loading branch information
d0p1s4m4 committed Jul 12, 2023
1 parent fd991da commit a9fec6e
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 91 deletions.
3 changes: 2 additions & 1 deletion kernel/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
KERNEL = vmstupid
SRCS = boot/head.s boot/feat.s kernel.s gdt.s pic.s isr.s idt.s pmm.s paging.s \
SRCS = boot/head.s \
kernel.s gdt.s pic.s isr.s idt.s \
lib/log.s drivers/serial.s
INCS = sys/multiboot.inc \
sys/i386/cpuid.inc \
Expand Down
55 changes: 0 additions & 55 deletions kernel/boot/feat.s

This file was deleted.

65 changes: 53 additions & 12 deletions kernel/boot/head.s
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
;; File: head.s

;;
;; About: CPU compatibility
;; /!\ Only work on *486+* for now.
;; - `invlpg` which is not part of 386 ISA.
;;
[BITS 32]

%include "machdep.inc"
%include "sys/multiboot.inc"
%include "sys/i386/registers.inc"
%include "sys/i386/mmu.inc"
Expand Down Expand Up @@ -38,32 +41,70 @@ section .multiboot.text
;;
global entry
entry:
extern stack_top
mov esp, V2P(stack_top)
;; disable interrupt
cli

;; save boot params
mov edi, eax
mov esi, ebx

extern feat_detect
call feat_detect

;; setup 4MB paging
;; TODO: check if 4MB paging is available
mov eax, cr4
or eax, CR4_PSE
mov cr4, eax

cmp edi, MB_MAGIC
jne .skip_map_multiboot
;; check if multiboot struct is in first 4Mib
;; otherwith add entry in page dir
mov eax, 400000
cmp ebx, eax
jg .map_multiboot
jmp .skip_map_multiboot
.map_multiboot:
;; TODO: for now let's assume data are bellow 4Mib
jmp .end
.skip_map_multiboot:
add esi, KERNBASE
.end:
extern page_directory
mov eax, V2P(page_directory)
mov eax, V2P(boot_page_dir)
mov cr3, eax

;; enable paging
mov eax, cr0
or eax, CR0_PG | CR0_PE | CR0_WP
mov cr0, eax

extern page_directory
mov eax, page_directory - KERNBASE
extern kernel_start
mov ebx, kernel_start
;; Jump to higher half
lea eax, entry_high
jmp eax ; near jump, indirect

section .text

entry_high:
mov dword [boot_page_dir], 0
invlpg [0]

extern stack_top
mov esp, stack_top
xor ebp, ebp

push edi
push esi
extern kmain
call kmain

cli
hang:
hlt
jmp hang

section .data
align 0x1000
boot_page_dir:
dd 0 | PDE_P | PDE_W | PDE_PS
times (P2PDE(KERNBASE) - 1) dd 0
dd 0 | PDE_P | PDE_W | PDE_PS
times 0x400 - (boot_page_dir - $$) dd 0
20 changes: 11 additions & 9 deletions kernel/kernel.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
; File: kernel.s
[BITS 32]

%include "machdep.inc"
%include "base.inc"

section .bss
align 16
Expand All @@ -17,14 +17,16 @@ kmain:
push ebp
mov ebp, esp

extern serial_init
call serial_init

LOG msg_hello_world

leave
ret

section .data
global machdep
machdep:
istruc machinfo
at machinfo.has_cpuid, db 0
at machinfo.has_pse, db 0
at machinfo.has_pae, db 0
iend
section .rodata

msg_hello_world db "StupidOS v", STUPID_VERSION, " (built with ", __NASM_VER__, " on ", __DATE__, " ", __TIME__, ")", 0

file db __FILE__, 0
2 changes: 1 addition & 1 deletion kernel/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SECTIONS

kernel_start = .;

.multiboot : {
.multiboot ALIGN(0x1000): {
KEEP(*(.multiboot.data))
KEEP(*(.multiboot.text))
}
Expand Down
5 changes: 0 additions & 5 deletions kernel/machdep.inc

This file was deleted.

10 changes: 7 additions & 3 deletions kernel/sys/i386/mmu.inc
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@
%define PDE2P(addr) (addr << 22)
%define P2PTE(addr) ((addr >> 12) & 0x3FF)

;; Defines: PDE
;; PTE_P - Present
;; Defines: Page Directory Flags
;; PDE_P - Present
;; PDE_W - Writable
;; PDE_U - User
;; PDE_PWT - Write-Through
;; PDE_PS - 4MiB page
PDE_P equ 1 << 0
PDE_W equ 1 << 1
PDE_U equ 1 << 2
Expand All @@ -57,7 +61,7 @@ PDE_D equ 1 << 6
PDE_PS equ 1 << 7
PDE_G equ 1 << 8

;; Defines: PTE
;; Defines: Page Table Flags
;; PTE_P - Present
;; PTE_W - Writable
;; PTE_U - User
Expand Down
15 changes: 11 additions & 4 deletions kernel/sys/multiboot.inc
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,18 @@ struc mb_info
endstruc

; Defines: flags
;
;
; MB_INFO_MEM - todo
; MB_INFO_BOOTDEV - todo
MB_INFO_MEM equ 1 << 0
MB_INFO_BOOTDEV equ 1 << 1
; MB_INFO_BOOTDEV - todo
MB_INFO_MEM equ 1 << 0
MB_INFO_BOOTDEV equ 1 << 1
MB_INFO_CMDLINE equ 1 << 2
MB_INFO_MODULES equ 1 << 3
MB_INFO_MMAP equ 1 << 6
MB_INFO_DRIVES equ 1 << 7
MB_INFO_BOOTLOADER_NAME equ 1 << 9
MB_INFO_VBE equ 1 << 11
MB_INFO_FRAMEBUFFER equ 1 << 12

; Structure: mb_mmap
struc mb_mmap
Expand Down
2 changes: 1 addition & 1 deletion lib/base/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ SRCS = log.s
INCS = base.inc

include $(TOPDIR)/share/mk/stupid.lib.mk
include $(TOPDIR)/share/mk/stupid.inc.mk
#include $(TOPDIR)/share/mk/stupid.inc.mk

0 comments on commit a9fec6e

Please sign in to comment.