Setup interrupt functions in 32 bits

This commit is contained in:
HyperAssembler 2015-01-27 00:59:50 -08:00
parent 3102815304
commit cac107ce81
4 changed files with 103 additions and 11 deletions

View File

@ -1,4 +1,5 @@
global hk_load_gdt
global hk_support_x64
SELECTOR_DATA_0 equ 3*8 + 0
SELECTOR_DATA_3 equ 4*8 + 3
SELECTOR_CODE_0 equ 1*8 + 0
@ -25,3 +26,12 @@ pop eax
mov ebp,esp
pop ebp
ret
;int hk_support_x64(void)
hk_support_x64:
push ebp
mov ebp,esp
mov esp,ebp
pop ebp

View File

@ -2,8 +2,11 @@
#include "kdef32.h"
#include "grub.h"
#include "mem32.h"
uint8 g_gdt[8*32];
uint8 g_gdt[8*8];
gdt_ptr g_gdt_ptr;
uint8 g_idt[8*256];
idt_ptr g_idt_ptr;
extern word* kernel_stack;
void HYPKERNEL32 print_str(char* str)
@ -13,9 +16,9 @@ void HYPKERNEL32 print_str(char* str)
{
*gram = (uint8)*str;
gram++;
*gram = 7;
gram++;
str++;
*gram = 7;
gram++;
str++;
}
return;
}
@ -40,15 +43,14 @@ int32 HYPKERNEL32 hk_main(multiboot_info_t* multiboot_info)
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;
hk_set_segment_descriptor(&g_gdt[32], &desc);
g_gdt_ptr.limit = 8*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;
}

View File

@ -3,7 +3,7 @@
int32 HYPKERNEL32 hk_set_segment_descriptor(uint8* const gdt, const segment_descriptor* const seg_desc)
{
if (gdt == NULL)
if (gdt == NULL || seg_desc == NULL)
return -1;
gdt[0] = (uint8)(seg_desc->limit & 0xFF);
gdt[1] = (uint8)((seg_desc->limit >> 8) & 0xFF);
@ -19,6 +19,52 @@ int32 HYPKERNEL32 hk_set_segment_descriptor(uint8* const gdt, const segment_desc
return 0;
}
int32 HYPKERNEL32 hk_set_interrupt_gate(uint8* const dst, const interrupt_gate* int_gate)
{
if(dst == NULL || int_gate == NULL)
return -1;
dst[0] = (uint8)(int_gate->offset & 0xFF);
dst[1] = (uint8)((int_gate->offset >> 8) & 0xFF);
dst[6] = (uint8)((int_gate->offset >> 16) & 0xFF);
dst[7] = (uint8)((int_gate->offset >> 24) & 0xFF);
dst[2] = (uint8)(int_gate->seg_sel & 0xFF);
dst[3] = (uint8)((int_gate->seg_sel >> 8) & 0xFF);;
dst[4] = 0;
dst[5] = (uint8)(6 + ((int_gate->Sz & 0x1) << 3) + ((int_gate->DPL & 0x3) << 5) + ((int_gate->Pr & 0x1 ) << 7));
return 0;
}
int32 HYPKERNEL32 hk_set_trap_gate(uint8* const dst, const trap_gate* tr_gate)
{
if(dst == NULL || tr_gate == NULL)
return -1;
dst[0] = (uint8)(tr_gate->offset & 0xFF);
dst[1] = (uint8)((tr_gate->offset >> 8) & 0xFF);
dst[6] = (uint8)((tr_gate->offset >> 16) & 0xFF);
dst[7] = (uint8)((tr_gate->offset >> 24) & 0xFF);
dst[2] = (uint8)(tr_gate->seg_sel & 0xFF);
dst[3] = (uint8)((tr_gate->seg_sel >> 8) & 0xFF);;
dst[4] = 0;
dst[5] = (uint8)(7 + ((tr_gate->Sz & 0x1) << 3) + ((tr_gate->DPL & 0x3) << 5) + ((tr_gate->Pr & 0x1 ) << 7));
return 0;
}
int32 HYPKERNEL32 hk_set_task_gate(uint8* const dst, const task_gate* int_gate)
{
if(dst == NULL || int_gate == NULL)
return -1;
dst[0] = 0;
dst[1] = 0;
dst[6] = 0;
dst[7] = 0;
dst[2] = (uint8)(int_gate->tss_sel & 0xFF);
dst[3] = (uint8)((int_gate->tss_sel >> 8) & 0xFF);
dst[4] = 0;
dst[5] = (uint8)(5 + ((int_gate->DPL & 0x3) << 5) + ((int_gate->Pr & 0x1 ) << 7));
return 0;
}
int32 HYPKERNEL32 hk_set_page_table_entry_32(uint32* dest,uint32 base,uint32 flags)
{
if (dest == NULL)

View File

@ -3,6 +3,31 @@
#include "type32.h"
#include "kdef32.h"
typedef struct __attribute__ ((packed))
{
uint32 offset;
uint16 seg_sel;
uint8 Pr;
uint8 DPL;
uint8 Sz;
} interrupt_gate;
typedef struct __attribute__ ((packed))
{
uint32 offset;
uint16 seg_sel;
uint8 Pr;
uint8 DPL;
uint8 Sz;
} trap_gate;
typedef struct __attribute__ ((packed))
{
uint16 tss_sel;
uint8 DPL;
uint8 Pr;
} task_gate;
typedef struct __attribute__ ((packed))
{
uint32 base;
@ -24,7 +49,16 @@ typedef struct __attribute__ ((packed))
uint32 base;
} gdt_ptr;
typedef struct __attribute__ ((packed))
{
uint16 limit;
uint32 base;
} idt_ptr;
int32 HYPKERNEL32 hk_set_segment_descriptor(uint8* const gdt, const segment_descriptor* const seg_desc);
extern void hk_load_gdt(gdt_ptr* ptr);
extern void hk_load_gdt(const gdt_ptr* const ptr);
int32 HYPKERNEL32 hk_set_interrupt_gate(uint8* const dst, const interrupt_gate* int_gate);
int32 HYPKERNEL32 hk_set_trap_gate(uint8* const dst, const trap_gate* tr_gate);
int32 HYPKERNEL32 hk_set_task_gate(uint8* const dst, const task_gate* int_gate);
#endif