Now actually runs.

alentours-dev
Sergiusz Bazanski 2011-02-21 17:29:53 +01:00
parent 1ba4954e58
commit 236f28e1c7
8 changed files with 95 additions and 32 deletions

3
.gdbinit Normal file
View File

@ -0,0 +1,3 @@
file kernel.bin
target remote localhost:1234
set step-mode on

View File

@ -19,7 +19,7 @@ CX:=$(ENV)/$(TARGET)-g++
AS:=nasm
LD:=$(ENV)/$(TARGET)-ld
CFLAGS:=-Wall -Wextra -Werror -nostdlib -nostartfiles -nodefaultlibs -std=c99
CFLAGS:=-Wall -Werror -nostdlib -nostartfiles -nodefaultlibs -std=c99 -g
CFLAGS+=-I ./include
LFLAGS:=-nostdlib -nostartfiles -nodefaultlibs
@ -60,8 +60,14 @@ hdd.img: kernel.bin
@mcopy -i hdd_temp.img dst/syslinux.cfg ::syslinux.cfg
@mv hdd_temp.img hdd.img
emulate-nohdd: kernel.bin
emulate-nohdd-debug: kernel.bin
@echo "[i] Starting GDB..."
@gnome-terminal -x /bin/bash -c "gdb"
@echo "[i] Starting QEmu..."
@qemu -kernel kernel.bin -S -gdb tcp::1234
emulate-nohdd: kernel.bin
@echo "[i] Starting QEMU..."
@qemu -kernel kernel.bin
emulate: hdd.img

View File

@ -1,6 +1,6 @@
#ifndef __PAGING_H__
#define __PAGING_H__
void init_simple_paging(void);
void paging_init_simple(void);
#endif

View File

@ -15,9 +15,11 @@ STACKSIZE equ 0x4000
; ############################## text segment #################################
; #############################################################################
section .text
section .setup
align 4
global g_before_gdt
; Multiboot header
MultiBootHeader:
dd MAGIC
@ -27,15 +29,18 @@ MultiBootHeader:
; Actual entry point
_start:
lgdt [falsegdt]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov cx, 0x10
mov ds, cx
mov es, cx
mov fs, cx
mov gs, cx
mov ss, cx
g_before_gdt:
jmp 0x08:higherhalf
section .text
align 4
higherhalf:
mov esp, kstack + STACKSIZE

View File

@ -6,8 +6,9 @@
// Real kernel entry point, called from _start.asm
void kmain(void *mbd, u32 magic)
{
init_simple_paging();
gdt_create_flat();
kclear();
kprintf("[i] Welcome to Cucumber!\n\n");
kprintf("[i] Magic from bootloader: 0x%x.\n", magic);
if (magic != 0x2BADB002)
{
@ -15,10 +16,7 @@ void kmain(void *mbd, u32 magic)
return;
}
char *szBootLoaderName = (char *)((u32 *)mbd)[16];
kclear();
kprintf("[i] Booting via %s.\n", szBootLoaderName);
paging_init_simple();
gdt_create_flat();
}

View File

@ -101,9 +101,9 @@ void kscroll_up(void)
if (g_kstdio_cur_y >= 25)
{
Temp = g_kstdio_cur_y - 25 + 1;
kmemcpy((void*)0xB8000, (void*)(0xB8000 - Temp * 80 * 2), (25 - Temp) * 80 * 2);
kmemcpy((void*)0xC00B8000, (void*)(0xC00B8000 - Temp * 80 * 2), (25 - Temp) * 80 * 2);
kmemsetw((void*)(0xB8000 + (25 - Temp) * 80), Blank, 80);
kmemsetw((void*)(0xC00B8000 + (25 - Temp) * 80), Blank, 80);
g_kstdio_cur_y = 25 - 1;
}
}
@ -175,7 +175,7 @@ void kdump(u8 *bData, u32 Length)
void kputch(s8 Character)
{
volatile u8 *VideoMemory = (u8 *)0xB8000;
volatile u8 *VideoMemory = (u8 *)0xC00B8000;
u16 Offset = (g_kstdio_cur_y * 80 + g_kstdio_cur_x) << 1;
if (Character == '\n')
@ -207,7 +207,7 @@ void kprint(s8 *szString)
void kclear(void)
{
volatile u8 *VideoMemory = (u8 *)0xB8000;
volatile u8 *VideoMemory = (u8 *)0xC00B8000;
u32 Size = (80 * 25 ) << 1;
for (u32 i = 0; i < Size; i += 2)
{

View File

@ -1,24 +1,58 @@
#include "Tier0/paging.h"
#include "Tier0/kstdio.h"
#include "types.h"
u32 g_kernel_page_directory[1024] __attribute__ ((aligned (4096)));
u32 g_low_page_table[1024] __attribute__ ((aligned (4096)));
void init_simple_paging(void)
/*u8 paging_get_physical(u32 Virtual, u32 *Physical)
{
u16 Index =
u32 DirectoryEntry = g_kernel_page_directory[Index];
u8 TablePresent = DirectoryEntry & 0b1;
if (!TablePresent)
return 0;
u32 TableAddress = 0;
TableAddress |= (DirectoryEntry & 0xFFFFF000);
u32 Table = *((u32 *) &TableAddress);
u8 PagePresent = DirectoryEntry &
}*/
void paging_dump_directory(void)
{
for (u32 i = 0; i < 10; i++)
{
kprintf("[i] Virtual 0x%X - 0x%X, Entry 0x%X.\n", i * 4096 * 1024, (i + 1) * 4096 * 1024, g_kernel_page_directory[i] & 0xFFFFF000);
}
}
void paging_init_simple(void)
{
void *RealKernelPageDir = (u8 *)g_kernel_page_directory + 0x40000000;
void *RealLowPageTable = (u8 *)g_low_page_table + 0x40000000;
for (u16 i = 0; i < 1024; i++)
{
g_kernel_page_directory[i] = (i * 4096) | 0x03;
g_low_page_table[i] = 0;
g_low_page_table[i] = (i * 4096) | 0x03;
g_kernel_page_directory[i] = 0;
}
kprintf("[i] Page Directory Physical: 0x%X, Virtual: 0x%X.\n", RealKernelPageDir, g_kernel_page_directory);
kprintf("[i] Low Page Table Physical: 0x%X, Virtual: 0x%X.\n", RealLowPageTable, g_low_page_table);
g_kernel_page_directory[0] = (u32)RealLowPageTable | 0x03;
g_kernel_page_directory[768] = (u32)RealLowPageTable | 0x03;
paging_dump_directory();
__asm__ volatile ( "mov %0, %%eax\n"
__asm volatile ( "mov %0, %%eax\n"
"mov %%eax, %%cr3\n"
"mov %%cr0, %%eax\n"
"orl $0x80000000, %%eax\n"

View File

@ -1,31 +1,48 @@
OUTPUT_FORMAT("elf32-i386")
ENTRY(_start)
Offset = 0x00100000;
LMA = 0x00000000;
VMA = 0xC0000000;
SECTIONS
{
. = 0x100000;
.setup :
. = Offset;
g_setup_gdt_ptr = g_gdt_ptr - VMA;
g_setup_gdt_entries = g_gdt_entries - VMA;
.setup LMA + Offset : AT(LMA + Offset)
{
*(.setup)
. = ALIGN(0x1000);
}
. += 0xC0000000;
. += VMA;
.text : AT(ADDR(.text) - 0xC0000000)
.text : AT(g_section_code)
{
g_section_code = . - VMA;
*(.text)
. = ALIGN(0x1000);
}
.data ALIGN (4096) : AT(ADDR(.data) - 0xC0000000)
.data : AT(g_section_data)
{
g_section_data = . - VMA;
*(.data)
*(.rodata*)
. = ALIGN(0x1000);
}
.bss ALIGN (4096) : AT(ADDR(.bss) - 0xC0000000)
.bss : AT(g_section_bss)
{
g_section_bss = . - VMA;
*(COMMON*)
*(.bss*)
. = ALIGN(0x1000);
}
. = ALIGN(0x1000);
}