Got the new gdt loading working and basic printing stuff.

Fixed script issue.
This commit is contained in:
HyperAssembler 2015-01-26 01:30:10 -08:00
parent 9f5fbdea4d
commit 3102815304
11 changed files with 176 additions and 95 deletions

View File

@ -20,8 +20,8 @@ ASM_FILES = $(wildcard $(ASM_SRC_PATH)/*.asm)
KERNEL_BIN = $(OUTPUT_DIR)/kernel.bin
#Object files
C_OBJ_FILES = $(addprefix $(OUTPUT_DIR)/,$(notdir $(C_FILES:.c=.o)))
ASM_OBJ_FILES = $(addprefix $(OUTPUT_DIR)/,$(notdir $(ASM_FILES:.asm=.o)))
C_OBJ_FILES = $(addprefix $(OUTPUT_DIR)/,$(notdir $(C_FILES:.c=.oc)))
ASM_OBJ_FILES = $(addprefix $(OUTPUT_DIR)/,$(notdir $(ASM_FILES:.asm=.oasm)))
ALL_OBJ_FILES = $(C_OBJ_FILES) $(ASM_OBJ_FILES)
@ -45,12 +45,11 @@ buildiso:
sudo cp $(GRUB_CFG) $(OUTPUT_DIR)/temp_iso/boot/grub/
sudo grub-mkrescue -o HOS.iso $(OUTPUT_DIR)/temp_iso
$(OUTPUT_DIR)/%.oc : $(C_SRC_PATH)/%.c
sudo $(CC) $(C_FLAGS_32) -o $@ $^
$(C_OBJ_FILES): $(C_FILES)
sudo $(CC) $(C_FLAGS_32) -o $@ $<
$(ASM_OBJ_FILES): $(ASM_FILES)
sudo $(ASM) $(ASM_FLAGS_32) -o $@ $<
$(OUTPUT_DIR)/%.oasm : $(ASM_SRC_PATH)/%.asm
sudo $(ASM) $(ASM_FLAGS_32) -o $@ $^
$(KERNEL_BIN): $(ALL_OBJ_FILES)
sudo $(LD) $(LD_FLAGS_32) -T $(LD_SCRIPT) -o $(KERNEL_BIN) $(ALL_OBJ_FILES)

27
x86/src/asm/asm_mem32.asm Normal file
View File

@ -0,0 +1,27 @@
global hk_load_gdt
SELECTOR_DATA_0 equ 3*8 + 0
SELECTOR_DATA_3 equ 4*8 + 3
SELECTOR_CODE_0 equ 1*8 + 0
SELECTOR_CODE_3 equ 2*8 + 3
[SECTION .text]
[BITS 32]
;void hk_load_gdt(gdt_ptr* ptr)
hk_load_gdt:
push ebp
mov ebp,esp
push eax
mov eax,[ss:ebp+8]
lgdt [eax]
;reload cs
jmp SELECTOR_CODE_0:.reload
.reload
mov eax,SELECTOR_DATA_0
mov ss,ax
mov es,ax
mov fs,ax
mov gs,ax
mov ds,ax
pop eax
mov ebp,esp
pop ebp
ret

View File

@ -1,4 +1,3 @@
global print_str
global kernel_stack
extern hk_main
[SECTION .multiboot]
@ -40,7 +39,7 @@ SLCT_DATA_0 equ DESC_FLAT_RW - DESC_VOID
;Message
DUMMY_MSG:
db 'Loaded by multiboot1!',0
db 'Kernel loaded by multiboot...',0
;stack
times 1024 db 0
@ -72,7 +71,6 @@ mov ax,SLCT_GRAPH_0
mov gs,ax
push DUMMY_MSG
xchg bx,bx
call print_str
add esp,4

View File

@ -1,9 +1,54 @@
extern void* kernel_stack;
extern void print_str(char* src);
int hk_main(void)
#include "type32.h"
#include "kdef32.h"
#include "grub.h"
#include "mem32.h"
uint8 g_gdt[8*32];
gdt_ptr g_gdt_ptr;
extern word* kernel_stack;
void HYPKERNEL32 print_str(char* str)
{
uint8* gram = (uint8*)0x0b8000;
while(*str != 0)
{
*gram = (uint8)*str;
gram++;
*gram = 7;
gram++;
str++;
}
return;
}
int32 HYPKERNEL32 hk_main(multiboot_info_t* multiboot_info)
{
segment_descriptor desc_dummy = {.DPL = 0, .Pr = 0, .x64 = 0, .Sys = 0, .type = 0, .Sz = 0, .limit = 0,.Gr = 0,.base = 0, .Avl = 0};
segment_descriptor desc = {.Gr = 1,.Pr = 1 ,.Sz = 1, .Avl = 0,.Sys = 1, .x64 = 0 ,.base = 0,.limit = 0xFFFFF};
//dummy descriptor
hk_set_segment_descriptor(&g_gdt[0], &desc_dummy);
//ring 0 code seg, non-conforming
desc.type = 10;
desc.DPL = 0;
hk_set_segment_descriptor(&g_gdt[8], &desc);
//ring 3 code seg
desc.DPL = 3;
hk_set_segment_descriptor(&g_gdt[16], &desc);
//ring 0 data RW
desc.DPL = 0;
desc.type = 2;
hk_set_segment_descriptor(&g_gdt[24], &desc);
//ring 3 data
desc.DPL = 3;
hk_set_segment_descriptor(&g_gdt[32], &desc);
BOCHS_MAGIC_BREAKPOINT
g_gdt_ptr.limit = 32*8-1;
g_gdt_ptr.base = (uint32)g_gdt;
BOCHS_MAGIC_BREAKPOINT
hk_load_gdt(&g_gdt_ptr);
char* msg = "Welcome to HYP OS 1.0";
print_str(msg);
BOCHS_MAGIC_BREAKPOINT
loop:
goto loop;
}
}

12
x86/src/c/kdef32.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef _HKDEF32_H_
#define _HKDEF32_H_
#define HYPKERNEL32 __attribute__((cdecl))
#define BOCHS_MAGIC_BREAKPOINT asm("xchg bx,bx");
#define NULL ((void*)0)
#define SEGMENT_SELECTOR(Index,RPL) (((Index) << 3) + (RPL))
#endif

47
x86/src/c/mem32.c Normal file
View File

@ -0,0 +1,47 @@
#include "mem32.h"
#include "kdef32.h"
int32 HYPKERNEL32 hk_set_segment_descriptor(uint8* const gdt, const segment_descriptor* const seg_desc)
{
if (gdt == NULL)
return -1;
gdt[0] = (uint8)(seg_desc->limit & 0xFF);
gdt[1] = (uint8)((seg_desc->limit >> 8) & 0xFF);
gdt[6] = gdt[6] | (uint8)((seg_desc->limit >> 16) & 0xFF);
gdt[2] = (uint8)(seg_desc->base & 0xFF);
gdt[3] = (uint8)((seg_desc->base >> 8) & 0xFF);
gdt[4] = (uint8)((seg_desc->base >> 16) & 0xFF);
gdt[7] = (uint8)((seg_desc->base >> 24) & 0xFF);
gdt[5] = (uint8)((seg_desc->type & 0xF) + ((seg_desc->Sys & 0x1) << 4) + ((seg_desc->DPL & 0x3) << 5) + ((seg_desc->Pr & 0x1 ) << 7));
gdt[6] = gdt[6] | (uint8)(((seg_desc->Avl & 0x1) + ((seg_desc->x64 & 0x1) << 1) + ((seg_desc->Sz & 0x1) << 2) + ((seg_desc->Gr & 0x1) << 3)) << 4);
return 0;
}
int32 HYPKERNEL32 hk_set_page_table_entry_32(uint32* dest,uint32 base,uint32 flags)
{
if (dest == NULL)
return -1;
*dest = (base & 0xFFFFF000) + (flags & 0x0FFF);
return 0;
}
int32 HYPKERNEL32 hk_set_page_directory_entry_32(uint32* dest, uint32 base, uint32 flags)
{
if (dest == NULL)
return -1;
*dest = (base & 0xFFFFF000) + (flags & 0x0FFF);
return 0;
}
int32 HYPKERNEL32 hk_map_physcial_address_32(uint32* page_directory_base, uint32 physical_addr, uint32 virtual_addr, uint32 flags)
{
if (page_directory_base == NULL)
return -1;
uint32 pde_idx = virtual_addr >> 22;
uint32 pde = *(page_directory_base + pde_idx * 4);
//uint32 page_table_base = ;
//uint32 pte_idx = ;
return 0;
}

30
x86/src/c/mem32.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef _MEM_32_H_
#define _MEM_32_H_
#include "type32.h"
#include "kdef32.h"
typedef struct __attribute__ ((packed))
{
uint32 base;
uint32 limit;
uint8 type;
uint8 DPL;
uint8 Gr;
uint8 Avl;
uint8 Pr;
uint8 Sz; //32 bits = 1, 16 bits = 0
uint8 x64;
uint8 Sys; //System = 0, code/data = 1
} segment_descriptor;
typedef struct __attribute__ ((packed))
{
uint16 limit;
uint32 base;
} gdt_ptr;
int32 HYPKERNEL32 hk_set_segment_descriptor(uint8* const gdt, const segment_descriptor* const seg_desc);
extern void hk_load_gdt(gdt_ptr* ptr);
#endif

View File

@ -8,4 +8,8 @@ typedef int int32;
typedef short int16;
typedef long long int64;
typedef char int8;
typedef uint8 byte;
typedef uint16 word;
typedef uint32 dword;
typedef uint64 qword;
#endif

View File

@ -1,42 +0,0 @@
#include "mem32.h"
#include "kdef32.h"
int32 HYPKERNEL32 hk_set_segment_descriptor(segment_descriptor* pseg_descriptor, uint32 base, uint32 limit, uint8 access, uint8 flags)
{
if (pseg_descriptor == NULL)
return -1;
pseg_descriptor->base_low = (uint16)(base & 0xFFFF);
pseg_descriptor->base_middle = (uint8)((base >> 16) & 0xFF);
pseg_descriptor->base_high = (uint8)((base >> 24) & 0xFF);
pseg_descriptor->limit_low = (uint16)(limit & 0xFFFF);
pseg_descriptor->access = (uint8)(access & 0xFF);
pseg_descriptor->limit_mid_flags = (uint8)(((flags & 0x0F) << 4) + ((limit >> 16) & 0x0F));
return 0;
}
int32 HYPKERNEL32 hk_set_page_table_entry_32(uint32* dest,uint32 base,uint32 flags)
{
if (dest == NULL)
return -1;
*dest = (base & 0xFFFFF000) + (flags & 0x0FFF);
return 0;
}
int32 HYPKERNEL32 hk_set_page_directory_entry_32(uint32* dest, uint32 base, uint32 flags)
{
if (dest == NULL)
return -1;
*dest = (base & 0xFFFFF000) + (flags & 0x0FFF);
return 0;
}
int32 HYPKERNEL32 hk_map_physcial_address_32(uint32* page_directory_base, uint32 physical_addr, uint32 virtual_addr, uint32 flags)
{
if (page_directory_base == NULL)
return -1;
uint32 pde_idx = virtual_addr >> 22;
uint32 pde = *(page_directory_base + pde_idx * 4);
uint32 page_table_base = ;
uint32 pte_idx = ;
}

View File

@ -1,39 +0,0 @@
#ifndef _MEM_32_H_
#define _MEM_32_H_
#include "type32.h"
#include "io32.h"
#pragma push()
#pragma pack(1)
typedef struct
{
uint16 limit_low;
uint16 base_low;
uint8 base_middle;
uint8 access;
uint8 limit_mid_flags;
uint8 base_high;
} segment_descriptor;
typedef struct
{
uint16 limit;
uint32 base;
} gdt_ptr;
typedef struct
{
} pde_32;
typedef struct
{
} pte_32;
#pragma pop()
int32 hk_set_segment_descriptor(segment_descriptor* pseg_descriptor, uint32 base, uint32 limit, uint8 access, uint8 flags);
#endif