Enable paging.

This commit is contained in:
Nicolás Ortega Froysa 2018-04-11 18:04:14 +02:00
parent 794c4e0dd2
commit 1f325b4854
No known key found for this signature in database
GPG Key ID: FEC70E3BAE2E69BF
3 changed files with 57 additions and 0 deletions

View File

@ -49,6 +49,7 @@ if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "i.86")
${SRCS}
src/kernel/arch/x86/gdt.s
src/kernel/arch/x86/gdt.c
src/kernel/arch/x86/paging.c
src/kernel/arch/x86/tty.c)
set(CMAKE_EXE_LINKER_FLAGS
"-T ${CMAKE_CURRENT_SOURCE_DIR}/src/kernel/arch/x86/linker.ld -ffreestanding -nostdlib")

View File

@ -67,6 +67,15 @@ _start:
movw $0x28, %ax
ltr %ax
# paging
call setup_paging # will return pointer to page directory
# load page directory
movl %eax, %cr3
# enable the paging
movl %cr0, %eax
orl $0x80000000, %eax
movl %eax, %cr0
boot_kernel:
# enter high-level kernel (C)
call kernel_main

View File

@ -0,0 +1,47 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stddef.h>
uint32_t page_dir[1024] __attribute__((aligned(4096)));
uint32_t page_table[1024] __attribute__((aligned(4096)));
uint32_t *setup_paging() {
/*
* set all pages to:
* - supervisor: only kernel-mode can access
* - write enabled: can be read and written
* - not present: the page table is not present
*/
for(size_t i = 0; i < 1024; ++i)
page_dir[i] = 0x00000002;
/*
* fill the 1024 entries of the table with the flags
* supervisor, write enabled, and PRESENT.
*/
for(size_t i = 0; i < 1024; ++i)
page_table[i] = (i * 0x1000) | 3;
// put page table in first directory and enable the present
// flag
page_dir[0] = ((uint32_t) page_table) | 3;
return page_dir;
}