diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ec2f32..82eaa2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") diff --git a/src/kernel/arch/x86/boot.s b/src/kernel/arch/x86/boot.s index cd5c47f..2ec51b6 100644 --- a/src/kernel/arch/x86/boot.s +++ b/src/kernel/arch/x86/boot.s @@ -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 diff --git a/src/kernel/arch/x86/paging.c b/src/kernel/arch/x86/paging.c new file mode 100644 index 0000000..7407849 --- /dev/null +++ b/src/kernel/arch/x86/paging.c @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * 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 . + */ + +#include +#include + +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; +}