Browse Source

0.431u-boot: switch to long mode

rain 5 năm trước cách đây
mục cha
commit
c72a4ab901
6 tập tin đã thay đổi với 212 bổ sung và 5 xóa
  1. 1 0
      LINES
  2. 1 1
      Makefile
  3. 3 0
      README.md
  4. 1 1
      boot/Makefile
  5. 171 3
      boot/loader.asm
  6. 35 0
      include/boot/paging.asm

+ 1 - 0
LINES

@@ -1,3 +1,4 @@
 2021-5-21 0.066: 66 lines, 1 files, rain
 2021-5-29 0.228: 228 lines, 2 files, rain
+2021-5-29 0.431: 431 lines, 3 files, rain
 

+ 1 - 1
Makefile

@@ -6,7 +6,7 @@ all: Makefile
 
 
 xnix.img: $(OBJS) Makefile
-	dd if=/dev/zero of=xnix.img bs=1048576 count=60
+	dd if=/dev/zero of=xnix.img bs=1024 count=1440
 	dd if=$(OBJS) of=xnix.img conv=notrunc seek=0
 
 

+ 3 - 0
README.md

@@ -18,3 +18,6 @@ source software, you can get its source code from
 0.228u-boot:  
     update `boot.asm`: it reads the second sector of boot-driver (loader) and jumps to it. 
     `loader.asm` puts a message 'Starting Loader' on screen, and die. 
+
+0.431u-boot:  
+    `loader.asm` switching to long mode. And it set page map to address 0x00000000. It mapped 4MB memories. 

+ 1 - 1
boot/Makefile

@@ -6,7 +6,7 @@ bootsect: boot.bin loader.bin
 
 
 %.bin: %.asm
-	nasm $*.asm -o $*.bin 
+	nasm $*.asm -I ../include/ -o $*.bin 
 
 clear:
 	rm *.bin 

+ 171 - 3
boot/loader.asm

@@ -20,6 +20,9 @@
 org 0x7e00
 
 
+%include "boot/paging.asm"
+
+
 _start:
 	xor ax, ax
 
@@ -34,10 +37,33 @@ _start:
 	mov si, message
 	call print
 
-.die:
-	hlt
-	jmp .die
+.open_a20:
+	; disable interrupts before we initial done (enable in kernel)
+
+	; A20 line uses to control memory higher than 1MB. we can't access mem > 1MB when it's closing. 
+	; So we must open it before switching to protect-mode. 
+
+	cli
 
+	in al, 0x92
+	or al, 0x02
+	out 0x92, al
+
+.to_prot:
+	; switch to protect-mode. 
+	
+	lgdt [gdt32_desc]
+	
+	; CR0 (control register 0) uses to control CPU. It's first bit PE controls Protect-mode Enable. 
+	mov eax, cr0
+	or eax, 1
+	mov cr0, eax
+
+	; CPU is in protect-mode now, but segment registers didn't initialze. 
+	
+	; We cannot 'mov cx, xxx'
+
+	jmp dword code32:init_prot
 
 
 print:
@@ -57,9 +83,151 @@ print:
 	ret
 
 
+bits 32
+
+init_prot:
+	; we set segment selectors for segment register (CS was set by far-jmp)
+	mov ax, data32
+
+	mov ds, ax
+	mov es, ax
+	mov fs, ax
+	mov gs, ax
+
+	mov ss, ax
+
+	mov sp, 0x00007c00
+
+	; now we switch to long-mode
+
+.to_long_mode:
+
+.init_paging:
+	; we set page map at 0x00000000 (a good address)
+
+	mov dword [0x00000000], 0x00001000 | PAGE_USER
+	mov dword [0x00000004], 0x00000000
+
+	mov dword [0x00000800], 0x00001000 | PAGE_USER
+	mov dword [0x00000804], 0x00000000
+
+	; and page directory pointer table
+
+	mov dword [0x00001000], 0x00002000 | PAGE_USER
+	mov dword [0x00001004], 0x00000000
+
+	; page directory entry (2 MB)
+
+	mov dword [0x00002000], 0x00000000 | PAGE_KERNEL
+	mov dword [0x00002004], 0x00000000
+
+	mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
+	mov dword [0x0000200c], 0x00000000
+
+.enable_long_mode:
+	lgdt [gdt64_desc]
+
+	mov ax, data64
+
+	mov ds, ax
+	mov es, ax
+	mov fs, ax
+	mov gs, ax
+	mov ss, ax
+
+	; open PAE (page map level 3)
+	mov eax, cr4
+	bts eax, 5
+	mov cr4, eax
+
+	; set Page Table Base Register
+
+	; mov eax, 0x00000000
+	xor eax, eax
+	mov cr3, eax
+
+	; enable long mode (must before paging)
+
+	mov ecx, 0xc0000080
+	rdmsr
+
+	bts eax, 8
+	wrmsr
+
+	; enable paging
+
+	mov eax, cr0
+
+	; PE again
+	or eax, 1
+	bts eax, 31
+	
+	mov cr0, eax
+
+	; okay! now set segment registers and init long mode
+	
+	jmp dword code64:init_long_mode
+
+
+bits 64
+
+init_long_mode:
+	mov ax, data64
+
+	mov es, ax
+	mov ds, ax
+	mov fs, ax
+	mov gs, ax
+	mov ss, ax
+
+.die:	hlt
+	jmp .die
+
+
 message: db "Starting Loader", 0x0d, 0x0a, 0x00
 
 
+alignb 16
+
+gdt32:
+	dd 0x00000000, 0x00000000
+
+gdt32_code:
+	dd 0x0000ffff, 0x00cf9a00
+
+gdt32_data:
+	dd 0x0000ffff, 0x00cf9200
+
+gdt32_len equ $ - gdt32
+
+gdt32_desc:
+	dw gdt32_len - 1
+	dd gdt32
+
+code32 equ gdt32_code - gdt32
+data32 equ gdt32_data - gdt32
+
+
+
+gdt64:
+	dq 0x0000000000000000
+
+gdt64_code:
+	dq 0x0020980000000000
+
+gdt64_data:
+	dq 0x0000920000000000
+
+gdt64_len equ $ - gdt64
+
+gdt64_desc:
+	dw gdt64_len - 1
+	dd gdt64
+
+code64 equ gdt64_code - gdt64
+data64 equ gdt64_data - gdt64
+
+
 times 510 - ($ - $$) db 0x00
 db 0x55, 0xaa
 

+ 35 - 0
include/boot/paging.asm

@@ -0,0 +1,35 @@
+; Copyright (C) 2021 Rain
+
+; This file is part of XNIX. 
+; XNIX is free software: you can redistribute it and/or modify 
+; it under the terms of the GNU General Public License as published by 
+; the Free Software Foundation, either version 3 of the License, or 
+; (at your option) and later version. 
+
+; XNIX is distributed in the hope that it will be useful, 
+; but WITHOUT ANY WARRANTY; without even the implied warranty of 
+; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+; GNU General Public License for more details. 
+
+; You should have received a copy of the GNU General Public License 
+; along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+
+
+
+
+PATT_PRESENT 	equ 0x0001
+PATT_RDWR	equ 0x0002
+PATT_USER	equ 0x0004
+PATT_CACHEMEM	equ 0x0008
+PATT_NOCACHE	equ 0x0010
+PATT_ACCESSED	equ 0x0020
+PATT_DIRTY	equ 0x0040
+PATT_PAT	equ 0x0080
+PATT_GLOBAL	equ 0x0100
+
+; 0x83 it is. 
+PAGE_KERNEL	equ PATT_PRESENT + PATT_RDWR + PATT_PAT
+
+; this is linux's 007. (8-based 007)
+PAGE_USER	equ PATT_PRESENT + PATT_RDWR + PATT_USER
+