2019-03-06 20:05:18 +01:00
|
|
|
;=----------------------------------------------------------------------------=;
|
|
|
|
; GNU GPL OS/K ;
|
|
|
|
; ;
|
|
|
|
; Desc: Memory management from protected mode ;
|
|
|
|
; (x86_64 architecture only) ;
|
|
|
|
; ;
|
|
|
|
; ;
|
|
|
|
; Copyright © 2018-2019 The OS/K Team ;
|
|
|
|
; ;
|
|
|
|
; This file is part of OS/K. ;
|
|
|
|
; ;
|
|
|
|
; OS/K 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) any later version. ;
|
|
|
|
; ;
|
|
|
|
; OS/K 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 OS/K. If not, see <https://www.gnu.org/licenses/>. ;
|
|
|
|
;=----------------------------------------------------------------------------=;
|
|
|
|
|
2019-05-16 23:56:23 +02:00
|
|
|
%define MAX_MEMORY 1 ; GiB
|
2019-03-27 17:29:39 +01:00
|
|
|
|
2019-03-06 20:05:18 +01:00
|
|
|
[BITS 32]
|
2019-03-12 22:11:00 +01:00
|
|
|
[section .text]
|
2019-03-06 20:05:18 +01:00
|
|
|
; ---------------------------------------------------------------------------- ;
|
|
|
|
; Constructor for the page tables in protected mode ;
|
|
|
|
; ---------------------------------------------------------------------------- ;
|
|
|
|
Setup_paging:
|
|
|
|
;; Map the first PML4 entry to PDP table
|
|
|
|
mov eax, PDP_table
|
2019-03-08 08:43:44 +01:00
|
|
|
or eax, 1 << 1 | 1 << 0 ; present + writable
|
2019-03-06 20:05:18 +01:00
|
|
|
mov [PML4_table], eax
|
|
|
|
|
2019-03-28 10:34:18 +01:00
|
|
|
;; Map the PDP entries to PD tables
|
|
|
|
mov ebx, PD_table ; start address
|
|
|
|
mov ecx, 0x0 ; counter variable
|
2019-03-27 17:29:39 +01:00
|
|
|
.map_pdp_table:
|
2019-03-28 10:34:18 +01:00
|
|
|
mov eax, ebx
|
2019-03-08 08:43:44 +01:00
|
|
|
or eax, 1 << 1 | 1 << 0 ; present + writable
|
2019-03-28 10:34:18 +01:00
|
|
|
mov [PDP_table + 8 * ecx], eax
|
2019-03-27 17:29:39 +01:00
|
|
|
inc ecx
|
2019-03-28 10:34:18 +01:00
|
|
|
add ebx, 4096
|
|
|
|
cmp ecx, MAX_MEMORY ; PDP table is mapped if MAX_MEMORY
|
|
|
|
jne .map_pdp_table ; else map the next entry
|
2019-03-06 20:05:18 +01:00
|
|
|
|
2019-05-15 02:26:55 +02:00
|
|
|
;; Map each PD entry to a 'huge' 4MiB page
|
2019-03-28 10:34:18 +01:00
|
|
|
|
2019-03-08 08:43:44 +01:00
|
|
|
mov ecx, 0x0 ; counter variable
|
2019-03-27 17:29:39 +01:00
|
|
|
.map_pd_table:
|
2019-05-15 02:26:55 +02:00
|
|
|
;; map ecx-th PD entry to a huge page that starts at address 4MiB*ecx
|
2019-03-06 20:05:18 +01:00
|
|
|
mov eax, 0x200000
|
2019-03-08 08:43:44 +01:00
|
|
|
mul ecx ; start address of ecx-th page
|
|
|
|
or eax, 1 << 7 | 1 << 1 | 1 << 0 ; present + writable + huge
|
2019-03-06 20:05:18 +01:00
|
|
|
mov [PD_table + ecx * 8], eax
|
|
|
|
inc ecx
|
2019-03-27 17:29:39 +01:00
|
|
|
cmp ecx, 512 * MAX_MEMORY ; PD table is mapped if 512
|
|
|
|
jne .map_pd_table ; else map the next entry
|
2019-05-15 02:26:55 +02:00
|
|
|
ret
|
2019-03-06 20:05:18 +01:00
|
|
|
|
2019-03-06 20:05:18 +01:00
|
|
|
; ---------------------------------------------------------------------------- ;
|
|
|
|
; Enable long mode and paging ;
|
|
|
|
; ---------------------------------------------------------------------------- ;
|
|
|
|
Go64:
|
2019-03-06 20:05:18 +01:00
|
|
|
pusha
|
|
|
|
|
2019-03-06 20:05:18 +01:00
|
|
|
;; Registering paging
|
|
|
|
mov eax, PML4_table
|
2019-03-08 08:43:44 +01:00
|
|
|
mov cr3, eax ; Load PML4 to cr3
|
2019-03-06 20:05:18 +01:00
|
|
|
|
|
|
|
mov eax, cr4
|
|
|
|
or eax, 1 << 5
|
2019-03-08 08:43:44 +01:00
|
|
|
mov cr4, eax ; Enable PAE
|
2019-03-06 20:05:18 +01:00
|
|
|
|
|
|
|
;; Activate long mode
|
2019-03-08 08:43:44 +01:00
|
|
|
mov ecx, 0xC0000080 ; Address of MSR
|
|
|
|
rdmsr ; Read MSR
|
|
|
|
or eax, 1 << 8 ; LME = 1. (Long Mode Enable)
|
2019-05-15 15:55:57 +02:00
|
|
|
or eax, 1 << 11 ; NXE = 1 (No execute bit)
|
|
|
|
|
2019-03-08 08:43:44 +01:00
|
|
|
wrmsr ; Write MSR
|
2019-03-06 20:05:18 +01:00
|
|
|
|
|
|
|
;; Enable paging
|
|
|
|
mov eax, cr0
|
2019-03-08 08:43:44 +01:00
|
|
|
or eax, 1 << 31 ; Make MSR bit 31 (PG = Paging) to 1 :
|
|
|
|
; |1|000000000000000000000000000000
|
|
|
|
; |
|
|
|
|
; `------ Paging bit
|
2019-03-06 20:05:18 +01:00
|
|
|
mov cr0, eax
|
2019-03-06 20:05:18 +01:00
|
|
|
jmp .end
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
.end:
|
2019-03-06 20:05:18 +01:00
|
|
|
|
2019-03-06 20:05:18 +01:00
|
|
|
popa
|
2019-03-06 20:05:18 +01:00
|
|
|
ret
|
2019-03-06 20:05:18 +01:00
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------- ;
|
|
|
|
; Checks if Grub has enabled A20 line ;
|
|
|
|
; ---------------------------------------------------------------------------- ;
|
|
|
|
CheckA20:
|
|
|
|
pushad
|
2019-03-08 08:43:44 +01:00
|
|
|
mov edi, 0x112345 ; Odd megabyte address.
|
|
|
|
mov esi, 0x012345 ; Even megabyte address.
|
|
|
|
mov [esi],esi ; Making sure that both addresses contain diffrent values.
|
|
|
|
mov [edi],edi ; (if A20 line is cleared the two pointers would point to the address 0x012345 that would contain 0x112345 (edi))
|
|
|
|
cmpsd ; Compare addresses to see if the're equivalent.
|
2019-03-06 20:05:18 +01:00
|
|
|
popad
|
2019-03-08 08:43:44 +01:00
|
|
|
jne .A20_on ; If not equivalent , A20 line is set.
|
2019-03-06 20:05:18 +01:00
|
|
|
jmp .A20_err
|
|
|
|
.A20_on:
|
|
|
|
ret
|
|
|
|
.A20_err:
|
2019-03-08 08:43:44 +01:00
|
|
|
mov ax, "04" ; ERROR 04 : A20 line failed
|
2019-03-06 20:05:18 +01:00
|
|
|
jmp Error
|
2019-03-25 23:10:06 +01:00
|
|
|
|
|
|
|
[BITS 64]
|
|
|
|
; ---------------------------------------------------------------------------- ;
|
|
|
|
; Initilizes the stack ;
|
2019-05-13 15:18:00 +02:00
|
|
|
; ;
|
|
|
|
; Not a function because it wipes the stack ;) ;
|
2019-03-25 23:10:06 +01:00
|
|
|
; ---------------------------------------------------------------------------- ;
|
2019-05-13 15:18:00 +02:00
|
|
|
; XXX : MAKE A PAGE GUARD FOR THE STACK
|
2019-03-25 23:10:06 +01:00
|
|
|
InitStack:
|
|
|
|
xor rax, rax ; "Fill pattern"
|
|
|
|
push rcx
|
|
|
|
push rdi
|
|
|
|
|
|
|
|
;; Begin address to fill and length
|
2019-05-15 15:55:57 +02:00
|
|
|
mov rdi, kernelEnd
|
2019-05-13 15:18:00 +02:00
|
|
|
|
2019-05-15 15:55:57 +02:00
|
|
|
;; Alignes it to 4096o / FUTURE PAGE FRAME
|
|
|
|
shr rdi, 12
|
|
|
|
shl rdi, 12
|
|
|
|
add rdi, 0x1000
|
2019-05-13 15:18:00 +02:00
|
|
|
|
2019-05-15 15:55:57 +02:00
|
|
|
;; Passing info to kernel
|
|
|
|
mov qword [newKernelEnd], rdi
|
|
|
|
mov rcx, KERNEL_STACK ; counter
|
2019-03-25 23:10:06 +01:00
|
|
|
|
|
|
|
;; If bit 0 is on, fill one byte
|
|
|
|
sar rcx, 1 ; Shift bit 0 into CY
|
|
|
|
jnc $ + 3
|
|
|
|
stosb
|
2019-03-27 17:29:39 +01:00
|
|
|
|
2019-03-25 23:10:06 +01:00
|
|
|
;; We are word aligned and if bit 1 was on fill another word
|
|
|
|
sar rcx, 1 ; Shift bit 1 into CY
|
|
|
|
jnc $ + 4
|
|
|
|
stosw
|
2019-03-27 17:29:39 +01:00
|
|
|
|
2019-03-25 23:10:06 +01:00
|
|
|
;; We are dword aligned and if bit 2 was on fill another dword
|
2019-05-15 15:55:57 +02:00
|
|
|
sar rcx, 1 ; Shift bit 2 into CY
|
|
|
|
jnc $ + 3
|
2019-03-25 23:10:06 +01:00
|
|
|
stosd
|
2019-05-13 15:18:00 +02:00
|
|
|
|
2019-03-25 23:10:06 +01:00
|
|
|
;; RCX now equals the number of qwords to fill
|
2019-05-15 15:55:57 +02:00
|
|
|
repnz stosq ; Finish by writing RCX qwords.
|
|
|
|
|
|
|
|
;; New info for the kernel
|
|
|
|
mov qword [newStackEnd], rdi
|
|
|
|
mov rsp, rdi
|
2019-03-25 23:10:06 +01:00
|
|
|
|
|
|
|
pop rdi
|
|
|
|
pop rcx
|
2019-05-13 15:18:00 +02:00
|
|
|
jmp AfterInitStack
|