Kernel, Loader: More messing around with paging.

alentours-dev
Sergiusz Bazanski 2011-08-27 15:11:48 +02:00
parent c2b7afe6db
commit f3a475fef7
7 changed files with 63 additions and 38 deletions

View File

@ -56,35 +56,31 @@ struct S_PAGING_ML4_ENTRY {
typedef struct S_PAGING_ML4_ENTRY T_PAGING_ML4_ENTRY;
// OS-defined structures - semi-loose.
// OS-defined structures
typedef struct {
T_PAGING_TAB_ENTRY Entries[512];
u64 PhysicalAddress;
} T_PAGING_TAB;
} __attribute__((packed)) T_PAGING_TAB;
typedef struct {
T_PAGING_DIR_ENTRY Entries[512]; // For use by CPU
T_PAGING_TAB *Children[512]; // For use by OS
u64 PhysicalAddress; // FOr use by OS when setting CPU
} T_PAGING_DIR;
T_PAGING_DIR_ENTRY Entries[512]; // For use by the CPU
} __attribute__((packed)) T_PAGING_DIR;
typedef struct {
T_PAGING_DPT_ENTRY Entries[512];
T_PAGING_DIR *Children[512];
u64 PhysicalAddress;
} T_PAGING_DPT;
T_PAGING_DPT_ENTRY Entries[512]; // For use by the CPU
} __attribute__((packed)) T_PAGING_DPT;
typedef struct {
T_PAGING_ML4_ENTRY Entries[512];
T_PAGING_DPT *Children[512];
u64 PhysicalAddress;
} T_PAGING_ML4;
T_PAGING_ML4_ENTRY Entries[512]; // For use by the CPU
} __attribute__((packed)) T_PAGING_ML4;
void paging_init_simple(u64 PhysicalVirtualOffset);
void paging_init_simple(u64 KernelPhysicalStart, u64 KernelPhysicalSize);
T_PAGING_ML4 * paging_get_ml4(void);
u8 paging_get_physical(u64 Virtual, u64 *Physical);
u8 paging_get_physical_ex(u64 Virtual, u64 *Physical,T_PAGING_ML4 *ML4);
void paging_use_ml4(T_PAGING_ML4 *ML4);
T_PAGING_ML4 *paging_get_kernel_ml4(void);
/*void paging_map_kernel_page(u64 Virtual, u64 Physical);
void paging_map_kernel_table(u64 Virtual, u64 Physical);
void paging_map_page(u64 Virtual, u64 Physical, T_PAGING_DIRECTORY *Directory,

View File

@ -11,6 +11,8 @@ struct S_LOAD_CONTEXT {
u64 KernelPhysicalStart;
u64 KernelPhysicalEnd;
u64 LoaderPhysicalStart;
u64 LoaderPhysicalEnd;
s8 LoaderName[80];
// VGA text mode 0

View File

@ -45,10 +45,15 @@ void kmain(u32 LoadContextAddress)
kprintf("[i] Booting via %s.\n", LoadContext->LoaderName);
kprintf("[i] Memory available: %uk.\n", system_get_memory_upper());
kprintf("[i] Kernel physical: %x-%x.\n", LoadContext->KernelPhysicalStart, LoadContext->KernelPhysicalEnd);
kprintf("[i] Loader physical: %x-%x.\n", LoadContext->LoaderPhysicalStart, LoadContext->LoaderPhysicalEnd);
kprintf("[i] Kernel virtual: %x-%x.\n", &_start, &_end);
//paging_init_simple();
paging_init_simple(LoadContext->KernelPhysicalStart, LoadContext->KernelPhysicalEnd - LoadContext->KernelPhysicalStart);
//kprintf("physical: %x\n", PhysicalData);
//paging_get_physical_ex(0xf0000000, &test, paging_get_kernel_ml4());
//kprintf("%x\n", test);
for (;;) {}
/*gdt_create_flat();

View File

@ -1,20 +1,23 @@
#include "Tier0/paging.h"
#include "Tier0/kstdio.h"
#include "Tier0/kstdlib.h"
#include "Tier0/panic.h"
#include "types.h"
// The basic structures for the first kernel thread...
// Since they are < 2Mib, their virtual addresses = their physical addresses -
// some offset given by the loader
//T_PAGING_ML4 g_paging_basic_ml4;
// These are the structures for mapping the lowest 2MiB
//T_PAGING_ML4 g_paging_basic_dpt_lowmap;
//T_PAGING_ML4 g_paging_basic_dir_lowmap;
//T_PAGING_ML4 g_paging_basic_tab_lowmap;
// These are the structures for mapping the kernel memory (2Mib)
//T_PAGING_ML4 g_paging_basic_dpt_kernel;
//T_PAGING_ML4 g_paging_basic_dir_kernel;
//T_PAGING_ML4 g_paging_basic_tab_kernel;
// Here's an overview of how it will look like:
//
// ML4 -> DPT -> - DIR0 -> 512x Tab -> 512x512x Page
// - DIR1 -> 512x Tab -> 512x512x Page
// - DIR2 -> 512x Tab -> 512x512x Page
// - DIR3 -> 512x Tab -> 512x512x Page
// This lets us have more-or-less dynamic paging of the whole first 32-bit
// memory space... Since we can't have true dynamic allocation of paging
// structures (as he wave no kernel heap, as we have no paging), we'll have to
// settle for this. The size of this whole mess should be around 8mib or so.
struct {
T_PAGING_ML4 *g_KernelML4; // This is allocated semi-dynamically
} __attribute__((packed)) g_KernelPaging;
T_PAGING_ML4 *paging_get_ml4(void)
{
@ -23,32 +26,36 @@ T_PAGING_ML4 *paging_get_ml4(void)
return (T_PAGING_ML4*)Address;
}
T_PAGING_ML4 *paging_get_kernel_ml4(void)
{
//return &g_KernelPaging.ML4;
return 0;
}
u8 paging_get_physical_ex(u64 Virtual, u64 *Physical, T_PAGING_ML4 *ML4)
{
u16 ML4Index = PAGING_GET_ML4_INDEX(Virtual);
/*u16 ML4Index = PAGING_GET_ML4_INDEX(Virtual);
u16 DPTIndex = PAGING_GET_DPT_INDEX(Virtual);
u16 DirIndex = PAGING_GET_DIR_INDEX(Virtual);
u16 TabIndex = PAGING_GET_TAB_INDEX(Virtual);
if (!ML4->Entries[ML4Index].Present)
return 1;
T_PAGING_DPT *DPT = ML4->Children[ML4Index];
if (!DPT->Entries[DPTIndex].Present)
return 1;
T_PAGING_DIR *Dir = DPT->Children[DPTIndex];
if (!Dir->Entries[DirIndex].Present)
return 1;
T_PAGING_TAB *Tab = Dir->Children[DirIndex];
if (!Tab->Entries[TabIndex].Present)
return 1;
(*Physical) = (Tab->Entries[TabIndex].Physical << 12) + PAGING_GET_PAGE_OFFSET(Virtual);
(*Physical) = (Tab->Entries[TabIndex].Physical << 12) + PAGING_GET_PAGE_OFFSET(Virtual);*/
return 0;
}
@ -57,13 +64,17 @@ u8 paging_get_physical(u64 Virtual, u64 *Physical)
{
T_PAGING_ML4 *ml4 = paging_get_ml4();
return paging_get_physical_ex(Virtual, Physical, ml4);
return 0;
}
// This initializes a very basic paging structure for the first kernel thread
void paging_init_simple(u64 PhysicalVirtualOffset)
// This initializes the paging structure for the first kernel thread
void paging_init_simple(u64 KernelPhysicalStart, u64 KernelPhysicalSize)
{
}
void paging_use_ml4(T_PAGING_ML4 *ML4)
{
//__asm volatile ( "mov %%rax, %%cr3\n" :: "a" (ML4->PhysicalAddress));
}
/*void paging_dump_directory(void)

View File

@ -14,6 +14,7 @@ SECTIONS
_code = .;
*(.text)
*(.rodata*)
. = ALIGN(4096);
}

View File

@ -9,6 +9,8 @@ struct S_LOAD_CONTEXT {
u64 KernelPhysicalStart;
u64 KernelPhysicalEnd;
u64 LoaderPhysicalStart;
u64 LoaderPhysicalEnd;
s8 LoaderName[80];
// VGA text mode 0
@ -28,6 +30,8 @@ u8 stdio_current_line = 0;
u8 stdio_cur_x = 0, stdio_cur_y = 0;
extern u64 omg64;
extern u64 _end;
extern u64 _start;
u32 *pJmpLoadAddres = (u32 *)(((u8 *)&omg64) + 1);
@ -467,6 +471,8 @@ u32 load(void *Multiboot, unsigned int Magic)
g_Context.VGATextModeUsed = 1;
g_Context.MultibootUsed = 1;
g_Context.MultibootHeader = (u32)Multiboot;
g_Context.LoaderPhysicalStart = (u32)&_start;
g_Context.LoaderPhysicalEnd = (u32)&_end;
puts("Load context 0x");
print_hex((u32)&g_Context);
@ -496,6 +502,6 @@ u32 load(void *Multiboot, unsigned int Magic)
g_Context.VGACursorX = stdio_cur_x;
g_Context.VGACursorY = stdio_cur_y;
*pJmpLoadAddres = Header->Entry;
return 1;
}

View File

@ -6,6 +6,8 @@ Offset = 0x00100000;
SECTIONS
{
. = Offset;
_start = .;
.text :
{
@ -37,5 +39,7 @@ SECTIONS
*(.comment)
*(.eh_frame)
}
_end = .;
}