Use C11 standard.

Use string literal(graceful) instead of casting(ugly) for integer constants defined in macros.
This commit is contained in:
HyperAssembler 2015-02-04 00:43:01 -08:00
parent c886d569cd
commit d0fe0da17d
8 changed files with 75 additions and 82 deletions

View File

@ -4,14 +4,14 @@ LD = ld
#x86 vars
C_SRC_PATH_32 = x86/src/c
ASM_SRC_PATH_32 = x86/src/asm
C_FLAGS_32 = -m32 -c -fno-stack-protector -fno-builtin -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -masm=intel -Wall -Wextra
C_FLAGS_32 = -m32 -std=c11 -c -fno-stack-protector -fno-builtin -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -masm=intel -Wall -Wextra
ASM_FLAGS_32 = -f elf32 -I $(ASM_SRC_PATH_32)/
LD_FLAGS_32 = -melf_i386
LD_SCRIPT_32 = build/link32.ld
#x64 vars
C_SRC_PATH_64 = x64/src/c
ASM_SRC_PATH_64 = x64/src/asm
C_FLAGS_64 = -m64 -c -fno-stack-protector -fno-builtin -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -masm=intel -Wall -Wextra
C_FLAGS_64 = -m64 -std=c11 -c -fno-stack-protector -fno-builtin -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -masm=intel -Wall -Wextra
ASM_FLAGS_64 = -f elf64 -I $(ASM_SRC_PATH_64)/
LD_FLAGS_64 = -melf_x86_64
LD_SCRIPT_64 = build/link64.ld

View File

@ -24,7 +24,7 @@ or eax, 1 << 8 ; Set the LM-bit which is the 9th bit (bit 8).
wrmsr ; Write to the model-specific register.
; let cr3 point at page table
mov eax,;page table addr
mov eax,0;page table addr
mov cr3,eax
; enable paging, enter compatibility mode
@ -34,7 +34,7 @@ mov cr0, eax ; Set control register 0 to the A
ret
; enter x64
lgdt [g_gdt_ptr_64]
;lgdt [g_gdt_ptr_64]
jmp 8:entry_64
[SECTION .text]

View File

@ -3,12 +3,8 @@
#define HYPKERNEL64 __attribute__((sysv_abi))
#define BOCHS_MAGIC_BREAKPOINT asm("xchg bx,bx");
#define SEG_SELECTOR(Index,RPL) (((Index) << 3) + (RPL))
#define HLT_CPU asm("hlt");
#define NULL ((void*)0)
#endif

View File

@ -3,49 +3,57 @@
#include "type.h"
#include "kdef.h"
#define PML4_PRESENT ((uint64_t)1 << 0)
#define PML4_WRITE ((uint64_t)1 << 1)
#define PML4_USER ((uint64_t)1 << 2)
#define PML4_WRITE_THROUGH ((uint64_t)1 << 3)
#define PML4_CACHE_DISABLED ((uint64_t)1 << 4)
#define PML4_ACCESSED ((uint64_t)1 << 5)
#define PML4_EXECUTION_DISABLED ((uint64_t)1 << 63)
#define PML4_PRESENT (1ull << 0)
#define PML4_WRITE (1ull << 1)
#define PML4_USER (1ull << 2)
#define PML4_WRITE_THROUGH (1ull << 3)
#define PML4_CACHE_DISABLED (1ull << 4)
#define PML4_ACCESSED (1ull << 5)
#define PML4_EXECUTION_DISABLED (1ull << 63)
#define PDPT_PRESENT ((uint64_t)1 << 0)
#define PDPT_WRITE ((uint64_t)1 << 1)
#define PDPT_USER ((uint64_t)1 << 2)
#define PDPT_WRITE_THROUGH ((uint64_t)1 << 3)
#define PDPT_CACHE_DISABLED ((uint64_t)1 << 4)
#define PDPT_ACCESSED ((uint64_t)1 << 5)
#define PDPT_EXECUTION_DISABLED ((uint64_t)1 << 63)
#define PDPT_PRESENT (1ull << 0)
#define PDPT_WRITE (1ull << 1)
#define PDPT_USER (1ull << 2)
#define PDPT_WRITE_THROUGH (1ull << 3)
#define PDPT_CACHE_DISABLED (1ull << 4)
#define PDPT_ACCESSED (1ull << 5)
#define PDPT_EXECUTION_DISABLED (1ull << 63)
#define PD_PRESENT ((uint64_t)1 << 0)
#define PD_WRITE ((uint64_t)1 << 1)
#define PD_USER ((uint64_t)1 << 2)
#define PD_WRITE_THROUGH ((uint64_t)1 << 3)
#define PD_CACHE_DISABLED ((uint64_t)1 << 4)
#define PD_ACCESSED ((uint64_t)1 << 5)
#define PD_EXECUTION_DISABLED ((uint64_t)1 << 63)
#define PD_PRESENT (1ull << 0)
#define PD_WRITE (1ull << 1)
#define PD_USER (1ull << 2)
#define PD_WRITE_THROUGH (1ull << 3)
#define PD_CACHE_DISABLED (1ull << 4)
#define PD_ACCESSED (1ull << 5)
#define PD_EXECUTION_DISABLED (1ull << 63)
#define PT_PRESENT ((uint64_t)1 << 0)
#define PT_WRITE ((uint64_t)1 << 1)
#define PT_USER ((uint64_t)1 << 2)
#define PT_WRITE_THROUGH ((uint64_t)1 << 3)
#define PT_CACHE_DISABLED ((uint64_t)1 << 4)
#define PT_ACCESSED ((uint64_t)1 << 5)
#define PT_DIRTY ((uint64_t)1 << 6)
#define PT_ATTRIBUTE_TABLE ((uint64_t)1 << 7)
#define PT_GLOBAL ((uint64_t)1 << 8)
#define PT_EXECUTION_DISABLED ((uint64_t)1 << 63)
#define PT_PRESENT (1ull << 0)
#define PT_WRITE (1ull << 1)
#define PT_USER (1ull << 2)
#define PT_WRITE_THROUGH (1ull << 3)
#define PT_CACHE_DISABLED (1ull << 4)
#define PT_ACCESSED (1ull << 5)
#define PT_DIRTY (1ull << 6)
#define PT_ATTRIBUTE_TABLE (1ull << 7)
#define PT_GLOBAL (1ull << 8)
#define PT_EXECUTION_DISABLED (1ull << 63)
#define SEG_GRANULARITY ((uint64_t)1 << 55)
#define SEG_LONG ((uint64_t)1 << 53)
#define SEG_DPL(dpl) (((uint64_t)(dpl) & 0x3) << 45)
#define SEG_PRESENT ((uint64_t)1 << 47)
#define SEG_CODE_DATA ((uint64_t)1 << 44)
#define SEG_TYPE(type) (((uint64_t)(type) & 0xF) << 40)
#define SEG_AVAILABLE ((uint64_t)1 << 52)
#define SEG_32_BITS ((uint64_t)1 << 54)
#define SEG_GRANULARITY (1ull << 55)
#define SEG_LONG (1ull << 53)
#define SEG_DPL_0 (0ull << 45)
#define SEG_DPL_1 (1ull << 45)
#define SEG_DPL_2 (2ull << 45)
#define SEG_DPL_3 (3ull << 45)
#define SEG_PRESENT (1ull << 47)
#define SEG_CODE_DATA (1ull << 44)
#define SEG_TYPE_DATA_RW (2ull << 40)
#define SEG_TYPE_DATA_R (0ull << 40)
#define SEG_TYPE_CODE_X (8ull << 40)
#define SEG_TYPE_CODE_XR (10ull << 40)
#define SEG_TYPE_CODE_XC (12ull << 40)
#define SEG_TYPE_CODE_XRC (14ull << 40)
#define SEG_AVAILABLE (1ull << 52)
#define SEG_32_BITS (1ull << 54)
typedef struct __attribute__ ((packed))
{

View File

@ -5,18 +5,6 @@
uint64_t text_pos;
void HYPKERNEL64 hk_map_page(void * base, uint64_t const physcial_addr, uint64_t const linear_addr, uint64_t attr)
{
if(physcial_addr << 52 || linear_addr << 52)
{
return;
}
uint64_t const pml4_idx = (linear_addr >> 39) & 0x1FF; //9bits
}
uint64_t HYPKERNEL64 hk_str_len(char const * str)
{
uint32_t length = 0;

View File

@ -17,20 +17,17 @@ void HYPKERNEL32 hk_init_x86(multiboot_info_t const * const multiboot_info)
//dummy descriptor
hk_write_segment_descriptor((void*)(&g_gdt[0]), 0, 0, 0);
//ring 0 code seg, non-conforming
hk_write_segment_descriptor((void*)(&g_gdt[8]), 0, 0xFFFFF, SEG_TYPE(10) | SEG_CODE_DATA | SEG_32_BITS | SEG_GRANULARITY | SEG_PRESENT | SEG_DPL(0));
hk_write_segment_descriptor((void*)(&g_gdt[8]), 0, 0xFFFFF, SEG_TYPE_CODE_XR | SEG_CODE_DATA | SEG_32_BITS | SEG_GRANULARITY | SEG_PRESENT | SEG_DPL_0);
//ring 3 code seg
hk_write_segment_descriptor((void*)(&g_gdt[16]), 0, 0xFFFFF, SEG_TYPE(10) | SEG_CODE_DATA | SEG_32_BITS | SEG_GRANULARITY | SEG_PRESENT | SEG_DPL(3));
hk_write_segment_descriptor((void*)(&g_gdt[16]), 0, 0xFFFFF, SEG_TYPE_CODE_XR | SEG_CODE_DATA | SEG_32_BITS | SEG_GRANULARITY | SEG_PRESENT | SEG_DPL_3);
//ring 0 data RW
hk_write_segment_descriptor((void*)(&g_gdt[24]), 0, 0xFFFFF, SEG_TYPE(2) | SEG_CODE_DATA | SEG_32_BITS | SEG_GRANULARITY | SEG_PRESENT | SEG_DPL(0));
hk_write_segment_descriptor((void*)(&g_gdt[24]), 0, 0xFFFFF, SEG_TYPE_DATA_RW | SEG_CODE_DATA | SEG_32_BITS | SEG_GRANULARITY | SEG_PRESENT | SEG_DPL_0);
//ring 3 data
hk_write_segment_descriptor((void*)(&g_gdt[24]), 0, 0xFFFFF, SEG_TYPE(2) | SEG_CODE_DATA | SEG_32_BITS | SEG_GRANULARITY | SEG_PRESENT | SEG_DPL(3));
hk_write_segment_descriptor((void*)(&g_gdt[32]), 0, 0xFFFFF, SEG_TYPE_DATA_RW | SEG_CODE_DATA | SEG_32_BITS | SEG_GRANULARITY | SEG_PRESENT | SEG_DPL_3);
g_gdt_ptr.limit = 8 * 8 - 1;
g_gdt_ptr.base = (uint32_t) g_gdt;
hk_load_gdt(&g_gdt_ptr, SEG_SELECTOR(1, 0), SEG_SELECTOR(3, 0));
hk_print_str(" - Done.\n\n");
BOCHS_MAGIC_BREAKPOINT
//check memory, definitely < 32 so we assume that
hk_print_str("*Checking memory information...\n");
if(multiboot_info->flags & (1 << 6))
@ -103,21 +100,21 @@ void HYPKERNEL32 hk_init_x86(multiboot_info_t const * const multiboot_info)
{
hk_print_str("Module information is currently unavailable.\n\n");
}
HLT_CPU
a:
goto a;
}
void HYPKERNEL32 hk_init_x64(multiboot_info_t const * const multiboot_info)
{
//CHECK MODULE AND LOAD ELF
HLT_CPU
a:
goto a;
}
void HYPKERNEL32 hk_main(multiboot_info_t const * const multiboot_info)
{
//init text_position
text_pos = 0;
hk_load_gdt(&g_gdt_ptr, SEG_SELECTOR(1, 0), SEG_SELECTOR(3, 0));
hk_print_str(" - Done.\n\n");
//detect architecture
hk_print_str("*Checking architecture...\n");

View File

@ -3,10 +3,6 @@
#define HYPKERNEL32 __attribute__((cdecl))
#define BOCHS_MAGIC_BREAKPOINT asm("xchg bx,bx");
#define HLT_CPU asm("hlt");
#define NULL ((void*)0)
#define SEG_SELECTOR(Index,RPL) (((Index) << 3) + (RPL))

View File

@ -3,14 +3,22 @@
#include "type.h"
#include "kdef.h"
#define SEG_GRANULARITY ((uint64_t)1 << 55)
#define SEG_LONG ((uint64_t)1 << 53)
#define SEG_DPL(dpl) (((uint64_t)(dpl) & 0x3) << 45)
#define SEG_PRESENT ((uint64_t)1 << 47)
#define SEG_CODE_DATA ((uint64_t)1 << 44)
#define SEG_TYPE(type) (((uint64_t)(type) & 0xF) << 40)
#define SEG_AVAILABLE ((uint64_t)1 << 52)
#define SEG_32_BITS ((uint64_t)1 << 54)
#define SEG_GRANULARITY (1ull << 55)
#define SEG_LONG (1ull << 53)
#define SEG_DPL_0 (0ull << 45)
#define SEG_DPL_1 (1ull << 45)
#define SEG_DPL_2 (2ull << 45)
#define SEG_DPL_3 (3ull << 45)
#define SEG_PRESENT (1ull << 47)
#define SEG_CODE_DATA (1ull << 44)
#define SEG_TYPE_DATA_RW (2ull << 40)
#define SEG_TYPE_DATA_R (0ull << 40)
#define SEG_TYPE_CODE_X (8ull << 40)
#define SEG_TYPE_CODE_XR (10ull << 40)
#define SEG_TYPE_CODE_XC (12ull << 40)
#define SEG_TYPE_CODE_XRC (14ull << 40)
#define SEG_AVAILABLE (1ull << 52)
#define SEG_32_BITS (1ull << 54)
typedef struct __attribute__ ((packed))
{