I would like to ask for some help with my kernel initialization code. Firstly, I want to enable paging as soon as possible. So, I created a separated section in the linker script to do it (.setup). Both boot.S and pgsetup.c linked to here.
linker.ld:
Code: Select all
OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
OUTPUT_ARCH(i386)
ENTRY (loader)
hhalf = 0xc0000000;
page_size = 4096;
SECTIONS
{
. = 0x00100000;
.setup :
{
__start_setup = .;
*(.setup)
*(.setup.data)
boot/boot.o(.bss)
boot/boot.o(COMMON)
__end_setup = .;
}
/* Higher half */
. += 0xc0000000;
.text : AT(ADDR(.text) - hhalf)
{
*(.text)
__start_cs_init = .;
*(.init.text)
__end_cs_init = .;
}
.rodata ALIGN(page_size) : AT(ADDR(.rodata) - hhalf)
{
__start_data_kernel = .;
*(.rodata*)
}
.data ALIGN(page_size) : AT(ADDR(.data) - hhalf)
{
*(.data)
}
.bss ALIGN(page_size) : AT(ADDR(.bss) - hhalf)
{
sbss = .;
*(COMMON)
*(.bss)
ebss = .;
__end_data_kernel = .;
}
}
pgsetup.c:
Code: Select all
#define __SETUP__
#include <page.h>
uint32_t page_directory[1024] __setup_data __align(PAGE_SIZE);
uint32_t page_table[1024] __setup_data __align(PAGE_SIZE);
void setup_paging(void) __setup;
void setup_paging(void)
{
register int i;
for (i = 0; i < 1024; i++) {
page_directory[i] = 0;
page_table[i] = 0;
/* Map the first 4MB and set the appropriate flags */
page_table[i] = (i * PAGE_SIZE) | PT_PRESENT | PT_RW;
}
page_directory[0] = (uint32_t)page_table | PD_RW | PD_PRESENT;
page_directory[768] = (uint32_t)page_table | PD_RW | PD_PRESENT;
__asm__ __volatile__("movl %0, %%cr3\n"
"movl %%cr0, %%eax\n"
"orl 0x80000000, %%eax\n"
"movl %%eax, %%cr0\n"
: /* No output */
: "b"((uint32_t)page_directory)
: "eax");
}
boot.S:
Code: Select all
# Multiboot header omitted...
.comm lower_stack, STACKSIZE/16, 32 # Low-stack, linked to .setup.bss
loader:
mov $(lower_stack + STACKSIZE/16), %esp
#mov %eax, (mbtable)
#mov %ebx, (chksig)
pushl %eax
pushl %ebx
call setup_paging
popl %eax
popl %ebx
movl $(higher_stack + STACKSIZE), %esp
pushl %eax
pushl %ebx
call kmain
hang:
hlt
jmp hang
Sorry for the long post, but I think all code is essential. You can find my repo at: github.com/akoskovacs/Akosix/
Thank you in advance!