That's it for today.... Need some rest.

This commit is contained in:
HyperAssembler 2015-02-01 20:53:54 -08:00
parent ed9190db61
commit 7810a74741
7 changed files with 276 additions and 182 deletions

View File

@ -2,9 +2,10 @@
#include "kdef.h"
#include "print.h"
#include "mem.h"
#include "multiboot.h"
extern uint64_t text_pos;
void HYPKERNEL64 hk_main(void)
void HYPKERNEL64 hk_main(multiboot_info_t* multiboot_info)
{
hk_clear_screen();
hk_print_str("Welcome to HYP OS. Kernel is now running in x64 mode.\n");

View File

@ -6,46 +6,46 @@
typedef struct __attribute__ ((packed))
{
uint8_t Pr;
uint8_t RW;
uint8_t USU;
uint8_t PWT;
uint8_t PCD;
uint8_t Acc;
uint8_t Sz; //must be 0
uint64_t Pr;
uint64_t RW;
uint64_t USU;
uint64_t PWT;
uint64_t PCD;
uint64_t Acc;
uint64_t Sz; //must be 0
uint64_t base; // Since 4KB-aligned, 12 bits are useless, 52(total) - 12 = 40 bits left
// will ignore the low 12 bits as well as the high 12 bits of this field
uint8_t XD;
uint64_t XD;
} pml4_entry_t, pdpt_entry_t, pd_entry_t;
typedef struct __attribute__ ((packed))
{
uint8_t Pr;
uint8_t RW;
uint8_t USU;
uint8_t PWT;
uint8_t PCD;
uint8_t Acc;
uint8_t dirty;
uint8_t PAT;
uint8_t Gl;
uint64_t Pr;
uint64_t RW;
uint64_t USU;
uint64_t PWT;
uint64_t PCD;
uint64_t Acc;
uint64_t dirty;
uint64_t PAT;
uint64_t Gl;
uint64_t base; // Since 4KB-aligned, 12 bits are useless, 52(total) - 12 = 40 bits left
// will ignore the low 12 bits as well as the high 12 bits of this field
uint8_t XD;
uint64_t XD;
} pt_entry_t;
typedef struct __attribute__ ((packed))
{
uint32_t base;
uint32_t limit;
uint8_t type;
uint8_t DPL;
uint8_t Gr;
uint8_t Acc;
uint8_t Pr;
uint8_t Sz; //32 bits = 1, 16 bits = 0
uint8_t x64;
uint8_t Sys; //System = 0, code/data = 1
uint64_t base;
uint64_t limit;
uint64_t type;
uint64_t DPL;
uint64_t Gr;
uint64_t Acc;
uint64_t Pr;
uint64_t Sz; //32 bits = 1, 16 bits = 0
uint64_t x64;
uint64_t Sys; //System = 0, code/data = 1
} segment_descriptor_t;
typedef struct __attribute__ ((packed))
@ -57,7 +57,7 @@ typedef struct __attribute__ ((packed))
typedef struct __attribute__ ((packed))
{
uint16_t limit;
uint32_t base;
uint64_t base;
} idt_ptr_t;
void HYPKERNEL64 hk_write_segment_descriptor(uint8_t *const gdt, segment_descriptor_t const *const seg_desc);

View File

@ -75,3 +75,9 @@ mov eax, cr0 ; Set the A-register to control r
or eax, 1 << 31 ; Set the PG-bit, which is bit 31.
mov cr0, eax ; Set control register 0 to the A-register.
ret
;void hk_flush_tlb(void)
hk_flush_tlb:
mov eax,cr3
mov cr3,eax
ret

View File

@ -3,43 +3,219 @@
#include "multiboot.h"
#include "mem.h"
#include "print.h"
uint8_t g_gdt_64[8 * 8];
gdt_ptr_64_t g_gdt_ptr_64;
uint8_t g_gdt[8 * 8];
gdt_ptr_t g_gdt_ptr;
gdt_ptr_64_t g_gdt_ptr_64;
//x86 stuff
uint8_t g_idt[8 * 256];
idt_ptr_t g_idt_ptr;
extern uint32_t text_pos;
extern int32_t * kernel_stack;
extern void hk_entry_comp(void);
void HYPKERNEL32 hk_init_x64()
void HYPKERNEL32 hk_init_x86(multiboot_info_t const * const multiboot_info)
{
segment_descriptor_t desc_dummy = {.DPL = 0, .Pr = 0, .x64 = 0, .Sys = 0, .type = 0, .Sz = 0, .limit = 0, .Gr = 0, .base = 0, .Acc = 0};
segment_descriptor_t desc = {.Gr = 1, .Pr = 1, .Sz = 1, .Acc = 0, .Sys = 1, .x64 = 0, .base = 0, .limit = 0xFFFFF};
hk_print_str("*Setting up GDT...");
//dummy descriptor
hk_write_segment_descriptor(&g_gdt[0], &desc_dummy);
//ring 0 code seg, non-conforming
desc.type = 10;
desc.DPL = 0;
hk_write_segment_descriptor(&g_gdt[8], &desc);
//ring 3 code seg
desc.DPL = 3;
hk_write_segment_descriptor(&g_gdt[16], &desc);
//ring 0 data RW
desc.DPL = 0;
desc.type = 2;
hk_write_segment_descriptor(&g_gdt[24], &desc);
//ring 3 data
desc.DPL = 3;
hk_write_segment_descriptor(&g_gdt[32], &desc);
g_gdt_ptr.limit = 8 * 8 - 1;
g_gdt_ptr.base = (uint32_t) g_gdt;
hk_load_gdt(&g_gdt_ptr, SEGMENT_SELECTOR(1, 0), SEGMENT_SELECTOR(3, 0));
hk_print_str(" - Done.\n\n");
//check memory, definitely < 32 so we assume that
hk_print_str("*Checking memory information...\n");
if(multiboot_info->flags & (1 << 6))
{
multiboot_memory_map_t const *mem_map = (multiboot_memory_map_t *) multiboot_info->mmap_addr;
uint32_t const mem_map_size = multiboot_info->mmap_length / sizeof(multiboot_memory_map_t);
hk_print_str("BaseAddr - Length - Type\n");
uint32_t total_available_mem = 0;
uint32_t total_reserved_mem = 0;
uint32_t i = 0;
for (i = 0; i < mem_map_size; i++)
{
hk_print_hex((uint32_t)((mem_map + i)->addr));
hk_print_str(" - ");
hk_print_hex((uint32_t)((mem_map + i)->len));
hk_print_str(" - ");
hk_print_hex((mem_map + i)->type);
hk_print_str("\n");
if((mem_map + i)->type == MULTIBOOT_MEMORY_RESERVED)
{
total_reserved_mem += (uint32_t) ((mem_map + i)->len);
}
else if ((mem_map + i)->type == MULTIBOOT_MEMORY_AVAILABLE)
{
total_available_mem += (uint32_t) ((mem_map + i)->len);
}
}
hk_print_str("Total available memory: ");
hk_print_int(total_available_mem);
hk_print_str("B = ");
hk_print_int(total_available_mem/1024);
hk_print_str("KB = ");
hk_print_int(total_available_mem/1024/1024);
hk_print_str("MB\n");
hk_print_str("Total reserved memory: ");
hk_print_int(total_reserved_mem);
hk_print_str("B = ");
hk_print_int(total_reserved_mem/1024);
hk_print_str("KB = ");
hk_print_int(total_reserved_mem/1024/1024);
hk_print_str("MB\n\n");
}
else
{
hk_print_str("Memory information is currently unavailable.\n\n");
}
//check modules
hk_print_str("*Checking loaded kernel modules...\n");
if(multiboot_info->flags & (1 << 3))
{
multiboot_module_t const * mods_list = (multiboot_module_t *)multiboot_info->mods_addr;
uint32_t const mods_count = multiboot_info->mods_count;
hk_print_int(mods_count);
hk_print_str(" module(s) loaded:\n");
hk_print_str("Name - StartAddr - EndAddr\n");
uint32_t i = 0;
for (i = 0; i < mods_count; i++)
{
hk_print_str((char *) (mods_list + i)->cmdline);
hk_print_str(" - ");
hk_print_hex((mods_list + i)->mod_start);
hk_print_str(" - ");
hk_print_hex((mods_list + i)->mod_end);
hk_print_str("\n");
}
hk_print_str("\n");
}
else
{
hk_print_str("Module information is currently unavailable.\n\n");
}
HLT_CPU
}
void HYPKERNEL32 hk_init_x64(multiboot_info_t const * const multiboot_info)
{
//Setup x64
segment_descriptor_t desc_dummy = {.DPL = 0, .Pr = 0, .x64 = 0, .Sys = 0, .type = 0, .Sz = 0, .limit = 0, .Gr = 0, .base = 0, .Acc = 0};
segment_descriptor_t desc = {.Gr = 1, .Pr = 1, .Sz = 0, .Acc = 0, .Sys = 1, .x64 = 1, .base = 0, .limit = 0};
hk_print_str("*Setting up GDT for x64...");
hk_print_str("*Setting up GDT ...");
//dummy descriptor
hk_write_segment_descriptor(&g_gdt_64[0], &desc_dummy);
hk_write_segment_descriptor(&g_gdt[0], &desc_dummy);
//ring 0 code seg, non-conforming
desc.type = 10;
desc.DPL = 0;
hk_write_segment_descriptor(&g_gdt_64[8], &desc);
hk_write_segment_descriptor(&g_gdt[8], &desc);
//ring 3 code seg
desc.DPL = 3;
hk_write_segment_descriptor(&g_gdt_64[16], &desc);
hk_write_segment_descriptor(&g_gdt[16], &desc);
//ring 0 data RW
desc.DPL = 0;
desc.type = 2;
hk_write_segment_descriptor(&g_gdt_64[24], &desc);
hk_write_segment_descriptor(&g_gdt[24], &desc);
//ring 3 data
desc.DPL = 3;
hk_write_segment_descriptor(&g_gdt_64[32], &desc);
hk_write_segment_descriptor(&g_gdt[32], &desc);
g_gdt_ptr_64.limit = 8 * 8 - 1;
g_gdt_ptr_64.base = (uint64_t)g_gdt_64;
g_gdt_ptr_64.base = (uint64_t)g_gdt;
hk_print_str(" - Done.\n\n");
//check memory, definitely < 32 so we assume that
hk_print_str("*Checking memory information...\n");
if(multiboot_info->flags & (1 << 6))
{
multiboot_memory_map_t const *mem_map = (multiboot_memory_map_t *) multiboot_info->mmap_addr;
uint32_t const mem_map_size = multiboot_info->mmap_length / sizeof(multiboot_memory_map_t);
hk_print_str("BaseAddr - Length - Type\n");
uint32_t total_available_mem = 0;
uint32_t total_reserved_mem = 0;
uint32_t i = 0;
for (i = 0; i < mem_map_size; i++)
{
hk_print_hex((uint32_t)((mem_map + i)->addr));
hk_print_str(" - ");
hk_print_hex((uint32_t)((mem_map + i)->len));
hk_print_str(" - ");
hk_print_hex((mem_map + i)->type);
hk_print_str("\n");
if((mem_map + i)->type == MULTIBOOT_MEMORY_RESERVED)
{
total_reserved_mem += (uint32_t) ((mem_map + i)->len);
}
else if ((mem_map + i)->type == MULTIBOOT_MEMORY_AVAILABLE)
{
total_available_mem += (uint32_t) ((mem_map + i)->len);
}
}
hk_print_str("Total available memory: ");
hk_print_int(total_available_mem);
hk_print_str("B = ");
hk_print_int(total_available_mem/1024);
hk_print_str("KB = ");
hk_print_int(total_available_mem/1024/1024);
hk_print_str("MB\n");
hk_print_str("Total reserved memory: ");
hk_print_int(total_reserved_mem);
hk_print_str("B = ");
hk_print_int(total_reserved_mem/1024);
hk_print_str("KB = ");
hk_print_int(total_reserved_mem/1024/1024);
hk_print_str("MB\n\n");
}
else
{
hk_print_str("Memory information is currently unavailable.\n\n");
}
//check modules
hk_print_str("*Checking loaded kernel modules...\n");
if(multiboot_info->flags & (1 << 3))
{
multiboot_module_t const * mods_list = (multiboot_module_t *)multiboot_info->mods_addr;
uint32_t const mods_count = multiboot_info->mods_count;
hk_print_int(mods_count);
hk_print_str(" module(s) loaded:\n");
hk_print_str("Name - StartAddr - EndAddr\n");
uint32_t i = 0;
for (i = 0; i < mods_count; i++)
{
hk_print_str((char *) (mods_list + i)->cmdline);
hk_print_str(" - ");
hk_print_hex((mods_list + i)->mod_start);
hk_print_str(" - ");
hk_print_hex((mods_list + i)->mod_end);
hk_print_str("\n");
}
hk_print_str("\n");
}
else
{
hk_print_str("Module information is currently unavailable.\n\n");
}
//Setup identity x64 PAE paging
hk_print_str("*Setting up paging for x64...");
@ -88,120 +264,19 @@ void HYPKERNEL32 hk_init_x64()
void HYPKERNEL32 hk_main(multiboot_info_t const * const multiboot_info)
{
//initialize text position
uint32_t i = 0;
//init text_position
text_pos = 0;
hk_print_str("Welcome to HYP OS! Please wait while we are gathering information...\n\n");
segment_descriptor_t desc_dummy = {.DPL = 0, .Pr = 0, .x64 = 0, .Sys = 0, .type = 0, .Sz = 0, .limit = 0, .Gr = 0, .base = 0, .Acc = 0};
segment_descriptor_t desc = {.Gr = 1, .Pr = 1, .Sz = 1, .Acc = 0, .Sys = 1, .x64 = 0, .base = 0, .limit = 0xFFFFF};
hk_print_str("*Setting up GDT...");
//dummy descriptor
hk_write_segment_descriptor(&g_gdt[0], &desc_dummy);
//ring 0 code seg, non-conforming
desc.type = 10;
desc.DPL = 0;
hk_write_segment_descriptor(&g_gdt[8], &desc);
//ring 3 code seg
desc.DPL = 3;
hk_write_segment_descriptor(&g_gdt[16], &desc);
//ring 0 data RW
desc.DPL = 0;
desc.type = 2;
hk_write_segment_descriptor(&g_gdt[24], &desc);
//ring 3 data
desc.DPL = 3;
hk_write_segment_descriptor(&g_gdt[32], &desc);
g_gdt_ptr.limit = 8 * 8 - 1;
g_gdt_ptr.base = (uint32_t) g_gdt;
hk_load_gdt(&g_gdt_ptr, SEGMENT_SELECTOR(1, 0), SEGMENT_SELECTOR(3, 0));
hk_print_str(" - Done.\n\n");
//check memory, definitely < 32 so we assume that
hk_print_str("*Checking memory information...\n");
if(multiboot_info->flags & (1 << 6))
{
multiboot_memory_map_t const *mem_map = (multiboot_memory_map_t *) multiboot_info->mmap_addr;
uint32_t const mem_map_size = multiboot_info->mmap_length / sizeof(multiboot_memory_map_t);
hk_print_str("BaseAddr - Length - Type\n");
uint32_t total_available_mem = 0;
uint32_t total_reserved_mem = 0;
for (i = 0; i < mem_map_size; i++)
{
hk_print_hex((uint32_t)((mem_map + i)->addr));
hk_print_str(" - ");
hk_print_hex((uint32_t)((mem_map + i)->len));
hk_print_str(" - ");
hk_print_hex((mem_map + i)->type);
hk_print_str("\n");
if((mem_map + i)->type == MULTIBOOT_MEMORY_RESERVED)
{
total_reserved_mem += (uint32_t) ((mem_map + i)->len);
}
else if ((mem_map + i)->type == MULTIBOOT_MEMORY_AVAILABLE)
{
total_available_mem += (uint32_t) ((mem_map + i)->len);
}
}
hk_print_str("Total available memory: ");
hk_print_int(total_available_mem);
hk_print_str("B = ");
hk_print_int(total_available_mem/1024);
hk_print_str("KB = ");
hk_print_int(total_available_mem/1024/1024);
hk_print_str("MB\n");
hk_print_str("Total reserved memory: ");
hk_print_int(total_reserved_mem);
hk_print_str("B = ");
hk_print_int(total_reserved_mem/1024);
hk_print_str("KB = ");
hk_print_int(total_reserved_mem/1024/1024);
hk_print_str("MB\n\n");
}
else
{
hk_print_str("Memory information is currently unavailable.\n\n");
}
//check modules
hk_print_str("*Checking loaded kernel modules...\n");
if(multiboot_info->flags & (1 << 3))
{
multiboot_module_t const * mods_list = (multiboot_module_t *)multiboot_info->mods_addr;
uint32_t const mods_count = multiboot_info->mods_count;
hk_print_int(mods_count);
hk_print_str(" module(s) loaded:\n");
hk_print_str("Name - StartAddr - EndAddr\n");
for (i = 0; i < mods_count; i++)
{
hk_print_str((char *) (mods_list + i)->cmdline);
hk_print_str(" - ");
hk_print_hex((mods_list + i)->mod_start);
hk_print_str(" - ");
hk_print_hex((mods_list + i)->mod_end);
hk_print_str("\n");
}
hk_print_str("\n");
}
else
{
hk_print_str("Module information is currently unavailable.\n\n");
}
//detect architecture
hk_print_str("*Checking architecture...\n");
if (hk_support_x64() == 0)
{
hk_print_str("Arch: x86.\n\n");
x86:
goto x86;
hk_init_x86(multiboot_info);
}
else
{
hk_print_str("Arch: x86_64.\n\n");
hk_init_x64();
hk_init_x64(multiboot_info);
}
return;
}

View File

@ -4,6 +4,7 @@
#define HYPKERNEL32 __attribute__((cdecl))
#define BOCHS_MAGIC_BREAKPOINT asm("xchg bx,bx");
#define HLT_CPU asm("hlt");
#define NULL ((void*)0)

View File

@ -1,6 +1,17 @@
#include "kdef.h"
#include "mem.h"
void HYPKERNEL32 hk_map_page_64(uint8_t * base, uint64_t const physcial_addr, uint64_t const linear_addr, pml4_entry_t* pml4_entry, pdpt_entry_t* pdpt_entry, pd_entry_t pd_entry_t, pt_entry_t* pt_entry)
{
// all the paging structures serve as supplementary, i.e. when the page entry does not exist.
if(physcial_addr << 52 || linear_addr << 52)
{
return;
}
uint64_t const pml4_idx = (linear_addr >> 39) & 0x1FF; //9bits
}
void HYPKERNEL32 hk_write_pml4_entry(uint8_t * const base, pml4_entry_t const * const p_entry)
{
if(base == NULL || p_entry == NULL)

View File

@ -5,71 +5,71 @@
typedef struct __attribute__ ((packed))
{
uint8_t Pr;
uint8_t RW;
uint8_t USU;
uint8_t PWT;
uint8_t PCD;
uint8_t Acc;
uint8_t Sz; //must be 0
uint32_t Pr;
uint32_t RW;
uint32_t USU;
uint32_t PWT;
uint32_t PCD;
uint32_t Acc;
uint32_t Sz; //must be 0
uint64_t base; // Since 4KB-aligned, 12 bits are useless, 52(total) - 12 = 40 bits left
// will ignore the low 12 bits as well as the high 12 bits of this field
uint8_t XD;
uint32_t XD;
} pml4_entry_t, pdpt_entry_t, pd_entry_t;
typedef struct __attribute__ ((packed))
{
uint8_t Pr;
uint8_t RW;
uint8_t USU;
uint8_t PWT;
uint8_t PCD;
uint8_t Acc;
uint8_t dirty;
uint8_t PAT;
uint8_t Gl;
uint32_t Pr;
uint32_t RW;
uint32_t USU;
uint32_t PWT;
uint32_t PCD;
uint32_t Acc;
uint32_t dirty;
uint32_t PAT;
uint32_t Gl;
uint64_t base; // Since 4KB-aligned, 12 bits are useless, 52(total) - 12 = 40 bits left
// will ignore the low 12 bits as well as the high 12 bits of this field
uint8_t XD;
uint32_t XD;
} pt_entry_t;
typedef struct __attribute__ ((packed))
{
uint32_t offset;
uint16_t seg_sel;
uint8_t Pr;
uint8_t DPL;
uint8_t Sz;
uint32_t seg_sel;
uint32_t Pr;
uint32_t DPL;
uint32_t Sz;
} interrupt_gate_t;
typedef struct __attribute__ ((packed))
{
uint32_t offset;
uint16_t seg_sel;
uint8_t Pr;
uint8_t DPL;
uint8_t Sz;
uint32_t seg_sel;
uint32_t Pr;
uint32_t DPL;
uint32_t Sz;
} trap_gate_t;
typedef struct __attribute__ ((packed))
{
uint16_t tss_sel;
uint8_t DPL;
uint8_t Pr;
uint32_t tss_sel;
uint32_t DPL;
uint32_t Pr;
} task_gate_t;
typedef struct __attribute__ ((packed))
{
uint32_t base;
uint32_t limit;
uint8_t type;
uint8_t DPL;
uint8_t Gr;
uint8_t Acc;
uint8_t Pr;
uint8_t Sz; //32 bits = 1, 16 bits = 0
uint8_t x64;
uint8_t Sys; //System = 0, code/data = 1
uint32_t type;
uint32_t DPL;
uint32_t Gr;
uint32_t Acc;
uint32_t Pr;
uint32_t Sz; //32 bits = 1, 16 bits = 0
uint32_t x64;
uint32_t Sys; //System = 0, code/data = 1
} segment_descriptor_t;