Basic virtual boot support(no actual boot protocol though) #2
@@ -364,6 +364,13 @@ proc main _efi_handle, _efi_table
|
|||||||
cld
|
cld
|
||||||
rep stosq
|
rep stosq
|
||||||
|
|
||||||
|
; Zero out the paging tables table_btr_p1-p3
|
||||||
|
mov rdi, table_btr_p1
|
||||||
|
mov rcx, 512*3
|
||||||
|
xor rax, rax
|
||||||
|
cld
|
||||||
|
rep stosq
|
||||||
|
|
||||||
; P4[P4_OFFSET(VIRT_KERNEL_BASE)] = P3 or flags
|
; P4[P4_OFFSET(VIRT_KERNEL_BASE)] = P3 or flags
|
||||||
mov rax, table_p3
|
mov rax, table_p3
|
||||||
or rax, 0x3 ; present, r/w
|
or rax, 0x3 ; present, r/w
|
||||||
@@ -402,6 +409,61 @@ proc main _efi_handle, _efi_table
|
|||||||
cmp rcx, [kernel_image_phys_end]
|
cmp rcx, [kernel_image_phys_end]
|
||||||
jb .fill_p1
|
jb .fill_p1
|
||||||
|
|
||||||
|
mov r10, .map_btr_start
|
||||||
|
and r10, -4096 ; get earliest page
|
||||||
|
|
||||||
|
;TODO:very evil, doesn't respect if we somehow have the same upper
|
||||||
|
; bits of addr as kernel
|
||||||
|
;we probably need to separate this logic into map_region_lessthan2mb
|
||||||
|
|
||||||
|
; P4[BTR_P4_OFFSET(.map_btr_start)] = BTR_P3
|
||||||
|
mov rax, table_btr_p3
|
||||||
|
or rax, 0x3 ; present, r/w
|
||||||
|
mov rdi, table_p4
|
||||||
|
mov r11, r10
|
||||||
|
shr r11, (39 - 3)
|
||||||
|
and r11, (0x1ff shl 3)
|
||||||
|
add rdi, r11
|
||||||
|
stosq
|
||||||
|
|
||||||
|
; BTR_P3[BTR_P3_OFFSET(.map_btr_start)] = BTR_P2
|
||||||
|
mov rax, table_btr_p2
|
||||||
|
or rax, 0x3 ; present, r/w
|
||||||
|
mov rdi, table_btr_p3
|
||||||
|
mov r11, r10
|
||||||
|
shr r11, (30 - 3)
|
||||||
|
and r11, (0x1ff shl 3)
|
||||||
|
add rdi, r11
|
||||||
|
stosq
|
||||||
|
|
||||||
|
; BTR_P2[BTR_P2_OFFSET(.map_btr_start)] = BTR_P1
|
||||||
|
mov rax, table_btr_p1
|
||||||
|
or rax, 0x3 ; present, r/w
|
||||||
|
mov rdi, table_btr_p2
|
||||||
|
mov r11, r10
|
||||||
|
shr r11, (21 - 3)
|
||||||
|
and r11, (0x1ff shl 3)
|
||||||
|
add rdi, r11
|
||||||
|
stosq
|
||||||
|
|
||||||
|
mov r11, .map_btr_end
|
||||||
|
and r11, -4096
|
||||||
|
add r11, 4096 ; get end page
|
||||||
|
.fill_btr_p1:
|
||||||
|
mov rax, r10
|
||||||
|
shr rax, 12
|
||||||
|
and rax, 0x1FF
|
||||||
|
shl rax, BSF 8 ; *8
|
||||||
|
add rax, table_btr_p1
|
||||||
|
mov rdi, r10
|
||||||
|
or rdi, 0x3
|
||||||
|
mov [rax], rdi
|
||||||
|
|
||||||
|
add r10, 4096
|
||||||
|
cmp r10, r11
|
||||||
|
jb .fill_btr_p1
|
||||||
|
; hopefully .map_btr_start-.map_btr_end is mapped now
|
||||||
|
|
||||||
; Before performing ExitBootServces need to get fresh memory map
|
; Before performing ExitBootServces need to get fresh memory map
|
||||||
mov rbx, [efi_table]
|
mov rbx, [efi_table]
|
||||||
mov r10, [rbx + EFI_SYSTEM_TABLE.BootServices]
|
mov r10, [rbx + EFI_SYSTEM_TABLE.BootServices]
|
||||||
@@ -427,18 +489,30 @@ proc main _efi_handle, _efi_table
|
|||||||
; и через эту функу замаппить и ядро и этот кусок rip+N.
|
; и через эту функу замаппить и ядро и этот кусок rip+N.
|
||||||
; А в ядре уже так будут функци маппинга любых регионов
|
; А в ядре уже так будут функци маппинга любых регионов
|
||||||
|
|
||||||
|
; TODO: is there a way to minimize this?
|
||||||
|
mov r10, [kernel_image_phys_end]
|
||||||
|
sub r10, [kernel_image_phys_base]
|
||||||
|
add r10, VIRT_KERNEL_BASE
|
||||||
|
;prepared kernel sp reg
|
||||||
|
|
||||||
mov rax, table_p4
|
mov rax, table_p4
|
||||||
mov cr3, rax
|
mov cr3, rax
|
||||||
|
; WE CANNOT USE STACK AFTER .map_btr_start(STACK IS NOT MAPPED AFTER mov cr3, rax)
|
||||||
|
.map_btr_start:
|
||||||
; lea rax, [rip]
|
; lea rax, [rip]
|
||||||
; jmp rax
|
; jmp rax
|
||||||
; fstcall efi_print_hex_fixed, table_p4
|
; fstcall efi_print_hex_fixed, table_p4
|
||||||
|
|
||||||
mov rax, [kernel_image_phys_base]
|
mov rax, VIRT_KERNEL_BASE + KERNEL64_HEADER.entry_point_offset
|
||||||
mov rax, [rax + KERNEL64_HEADER.entry_point_offset]
|
mov rax, [rax]
|
||||||
add rax, [kernel_image_phys_base] ; jump to phys kernel for now
|
; add rax, [kernel_image_phys_base] ; jump to phys kernel for now
|
||||||
; add rax, VIRT_KERNEL_BASE
|
add rax, VIRT_KERNEL_BASE
|
||||||
|
mov rsp, r10
|
||||||
|
; we have set the kernel's stack
|
||||||
|
; now jump to it
|
||||||
|
;TODO:we definitely need to think about the memory map stuff
|
||||||
jmp rax
|
jmp rax
|
||||||
|
.map_btr_end:
|
||||||
|
|
||||||
; TODO
|
; TODO
|
||||||
;; QUESTION: How to pass memory map to the kernel?
|
;; QUESTION: How to pass memory map to the kernel?
|
||||||
@@ -563,5 +637,8 @@ table_p2 rq 512
|
|||||||
table_p3 rq 512
|
table_p3 rq 512
|
||||||
table_p4 rq 512
|
table_p4 rq 512
|
||||||
|
|
||||||
|
table_btr_p1 rq 512
|
||||||
|
table_btr_p2 rq 512
|
||||||
|
table_btr_p3 rq 512
|
||||||
|
|
||||||
section '.reloc' fixups data discardable
|
section '.reloc' fixups data discardable
|
||||||
|
Reference in New Issue
Block a user