Wow.... We are indeed in long mode. Although the current implementation is ugly. Still historical!!

This commit is contained in:
HyperAssembler 2015-02-01 14:14:44 -08:00
parent 510fd9e116
commit ed9190db61
17 changed files with 631 additions and 203 deletions

View File

@ -3,7 +3,7 @@ SECTIONS
{
.text.start(0x1000000) :
{
*(.multiboot)
*(.entry)
*(.text)
}

View File

@ -1,8 +1,9 @@
OUTPUT_FORMAT(binary)
SECTIONS
{
.text :
.text.start(0x100000) :
{
*(.entry)
*(.text)
}

View File

@ -33,9 +33,9 @@ KERNEL_BIN_64 = $(OUTPUT_DIR)/kernel64.bin
#Object files
C_OBJ_FILES_32 = $(addprefix $(OUTPUT_DIR)/,$(notdir $(C_FILES_32:.c=_x86.o)))
C_OBJ_FILES_64 = $(addprefix $(OUTPUT_DIR)/,$(notdir $(C_FILES_64:.c=_x86_64.o)))
C_OBJ_FILES_64 = $(addprefix $(OUTPUT_DIR)/,$(notdir $(C_FILES_64:.c=_x64.o)))
ASM_OBJ_FILES_32 = $(addprefix $(OUTPUT_DIR)/,$(notdir $(ASM_FILES_32:.asm=_asm_x86.o)))
ASM_OBJ_FILES_64 = $(addprefix $(OUTPUT_DIR)/,$(notdir $(ASM_FILES_64:.asm=_asm_x86_64.o)))
ASM_OBJ_FILES_64 = $(addprefix $(OUTPUT_DIR)/,$(notdir $(ASM_FILES_64:.asm=_asm_x64.o)))
ALL_OBJ_FILES_32 = $(C_OBJ_FILES_32) $(ASM_OBJ_FILES_32)
ALL_OBJ_FILES_64 = $(C_OBJ_FILES_64) $(ASM_OBJ_FILES_64)
@ -63,13 +63,13 @@ buildiso:
$(OUTPUT_DIR)/%_x86.o: $(C_SRC_PATH_32)/%.c
sudo $(CC) $(C_FLAGS_32) -o $@ $^
$(OUTPUT_DIR)/%_x86_64.o: $(C_SRC_PATH_64)/%.c
$(OUTPUT_DIR)/%_x64.o: $(C_SRC_PATH_64)/%.c
sudo $(CC) $(C_FLAGS_64) -o $@ $^
$(OUTPUT_DIR)/%_asm_x86.o: $(ASM_SRC_PATH_32)/%.asm
sudo $(ASM) $(ASM_FLAGS_32) -o $@ $^
$(OUTPUT_DIR)/%_asm_x86_64.o: $(ASM_SRC_PATH_64)/%.asm
$(OUTPUT_DIR)/%_asm_x64.o: $(ASM_SRC_PATH_64)/%.asm
sudo $(ASM) $(ASM_FLAGS_64) -o $@ $^
$(KERNEL_BIN_32): $(ALL_OBJ_FILES_32)

View File

@ -1,4 +1,18 @@
extern hk_main
[SECTION .text]
[SECTION .entry]
[BITS 64]
call hk_main
cli
;hard code for now
mov ax,24
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov ss,ax
;well align 16 bytes like this for now
mov rax,rsp
and rax,0xFFFFFFFFFFFFFFF0
mov rsp,rax
;no params for now
call hk_main
hlt

View File

@ -1,11 +1,13 @@
#include "type.h"
#include "kdef.h"
#include "print.h"
#include "mem.h"
extern word *kernel_stack;
int64_t HYPKERNEL64 hk_main(void)
extern uint64_t text_pos;
void HYPKERNEL64 hk_main(void)
{
//Setup x64
hk_clear_screen();
hk_print_str("Welcome to HYP OS. Kernel is now running in x64 mode.\n");
x64:
goto x64;
}

116
x64/src/c/mem.c Normal file
View File

@ -0,0 +1,116 @@
#include "kdef.h"
#include "mem.h"
void HYPKERNEL64 hk_write_pml4_entry(uint8_t * const base, pml4_entry_t const * const p_entry)
{
if(base == NULL || p_entry == NULL)
return;
base[0] = (uint8_t)((p_entry->Pr & 0x1) + ((p_entry->RW & 0x1) << 1) + ((p_entry->USU & 0x1) << 2) + ((p_entry->PWT & 0x1) << 3) + ((p_entry->PCD & 0x1) << 4) + ((p_entry->Acc & 0x1) << 5) + ((p_entry->Sz & 0x1) << 7));
base[1] = (uint8_t)(((p_entry->base >> 12) & 0xF) << 4); // 4 bits
base[2] = (uint8_t)((p_entry->base >> 16) & 0xFF); // 8 bits
base[3] = (uint8_t)((p_entry->base >> 24) & 0xFF); // 8 bits
base[4] = (uint8_t)((p_entry->base >> 32) & 0xFF); // 8 bits
base[5] = (uint8_t)((p_entry->base >> 40) & 0xFF); // 8 bits
base[6] = (uint8_t)((p_entry->base >> 48) & 0xF); // 4 bits
base[7] = (uint8_t)((p_entry->XD & 0x1) << 7);
return;
}
void HYPKERNEL64 hk_write_pdpt_entry(uint8_t * const base, pdpt_entry_t const * const p_entry)
{
if(base == NULL || p_entry == NULL)
return;
base[0] = (uint8_t)((p_entry->Pr & 0x1) + ((p_entry->RW & 0x1) << 1) + ((p_entry->USU & 0x1) << 2) + ((p_entry->PWT & 0x1) << 3) + ((p_entry->PCD & 0x1) << 4) + ((p_entry->Acc & 0x1) << 5) + ((p_entry->Sz & 0x1) << 7));
base[1] = (uint8_t)(((p_entry->base >> 12) & 0xF) << 4); // 4 bits
base[2] = (uint8_t)((p_entry->base >> 16) & 0xFF); // 8 bits
base[3] = (uint8_t)((p_entry->base >> 24) & 0xFF); // 8 bits
base[4] = (uint8_t)((p_entry->base >> 32) & 0xFF); // 8 bits
base[5] = (uint8_t)((p_entry->base >> 40) & 0xFF); // 8 bits
base[6] = (uint8_t)((p_entry->base >> 48) & 0xF); // 4 bits
base[7] = (uint8_t)((p_entry->XD & 0x1) << 7);
return;
}
void HYPKERNEL64 hk_write_pd_entry(uint8_t * const base, pd_entry_t const * const p_entry)
{
if(base == NULL || p_entry == NULL)
return;
base[0] = (uint8_t)((p_entry->Pr & 0x1) + ((p_entry->RW & 0x1) << 1) + ((p_entry->USU & 0x1) << 2) + ((p_entry->PWT & 0x1) << 3) + ((p_entry->PCD & 0x1) << 4) + ((p_entry->Acc & 0x1) << 5) + ((p_entry->Sz & 0x1) << 7));
base[1] = (uint8_t)(((p_entry->base >> 12) & 0xF) << 4); // 4 bits
base[2] = (uint8_t)((p_entry->base >> 16) & 0xFF); // 8 bits
base[3] = (uint8_t)((p_entry->base >> 24) & 0xFF); // 8 bits
base[4] = (uint8_t)((p_entry->base >> 32) & 0xFF); // 8 bits
base[5] = (uint8_t)((p_entry->base >> 40) & 0xFF); // 8 bits
base[6] = (uint8_t)((p_entry->base >> 48) & 0xF); // 4 bits
base[7] = (uint8_t)((p_entry->XD & 0x1) << 7);
return;
}
void HYPKERNEL64 hk_write_pt_entry(uint8_t * const base, pt_entry_t const * const p_entry)
{
if(base == NULL || p_entry == NULL)
return;
base[0] = (uint8_t)((p_entry->Pr & 0x1) + ((p_entry->RW & 0x1) << 1) + ((p_entry->USU & 0x1) << 2) + ((p_entry->PWT & 0x1) << 3) + ((p_entry->PCD & 0x1) << 4) + ((p_entry->Acc & 0x1) << 5) + ((p_entry->dirty & 0x1) << 6) + ((p_entry->PAT & 0x1) << 7));
base[1] = (uint8_t)((((p_entry->base >> 12) & 0xF) << 4) + (p_entry->Gl & 0x1)); // 4 bits
base[2] = (uint8_t)((p_entry->base >> 16) & 0xFF); // 8 bits
base[3] = (uint8_t)((p_entry->base >> 24) & 0xFF); // 8 bits
base[4] = (uint8_t)((p_entry->base >> 32) & 0xFF); // 8 bits
base[5] = (uint8_t)((p_entry->base >> 40) & 0xFF); // 8 bits
base[6] = (uint8_t)((p_entry->base >> 48) & 0xF); // 4 bits
base[7] = (uint8_t)((p_entry->XD & 0x1) << 7);
return;
}
void HYPKERNEL64 hk_write_segment_descriptor(uint8_t *const gdt, segment_descriptor_t const *const seg_desc)
{
if (gdt == NULL || seg_desc == NULL)
return;
gdt[0] = (uint8_t)(seg_desc->limit & 0xFF);
gdt[1] = (uint8_t)((seg_desc->limit >> 8) & 0xFF);
gdt[6] = gdt[6] | (uint8_t)((seg_desc->limit >> 16) & 0xFF);
gdt[2] = (uint8_t)(seg_desc->base & 0xFF);
gdt[3] = (uint8_t)((seg_desc->base >> 8) & 0xFF);
gdt[4] = (uint8_t)((seg_desc->base >> 16) & 0xFF);
gdt[7] = (uint8_t)((seg_desc->base >> 24) & 0xFF);
gdt[5] = (uint8_t)((seg_desc->type & 0xF) + ((seg_desc->Sys & 0x1) << 4) + ((seg_desc->DPL & 0x3) << 5) + ((seg_desc->Pr & 0x1 ) << 7));
gdt[6] = gdt[6] | (uint8_t)(((seg_desc->Acc & 0x1) + ((seg_desc->x64 & 0x1) << 1) + ((seg_desc->Sz & 0x1) << 2) + ((seg_desc->Gr & 0x1) << 3)) << 4);
return;
}
void HYPKERNEL64 hk_mem_cpy(void* src, void* dst, uint64_t size)
{
if (src == NULL || dst == NULL)
return;
char* cSrc = (char*)src;
char* cDst = (char*)dst;
while (size--)
*(cDst++) = *(cSrc++);
return;
}
void HYPKERNEL64 hk_mem_set(void* src, int8_t const val,uint64_t size)
{
if (src == NULL)
return;
while (size--)
*((int8_t*)src++) = val;
return;
}
void HYPKERNEL64 hk_mem_move(void* src, void* dst, uint64_t size)
{
if (src == NULL || dst == NULL)
return;
if (src >= dst)
{
return hk_mem_cpy(src,dst,size);
}
src += size;
dst += size;
while (size--)
*((char*)--dst) = *((char*)--src);
return;
}

81
x64/src/c/mem.h Normal file
View File

@ -0,0 +1,81 @@
#ifndef _MEM_H_
#define _MEM_H_
#include "type.h"
#include "kdef.h"
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 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;
} 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 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;
} 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
} segment_descriptor_t;
typedef struct __attribute__ ((packed))
{
uint16_t limit;
uint64_t base;
} gdt_ptr_t;
typedef struct __attribute__ ((packed))
{
uint16_t limit;
uint32_t base;
} idt_ptr_t;
void HYPKERNEL64 hk_write_segment_descriptor(uint8_t *const gdt, segment_descriptor_t const *const seg_desc);
//extern void HYPKERNEL64 hk_load_gdt(gdt_ptr_t const *const ptr, uint16_t const sel_code, uint16_t const sel_data);
void HYPKERNEL64 hk_mem_cpy(void *src, void *dst, uint64_t size);
void HYPKERNEL64 hk_mem_move(void *src, void *dst, uint64_t size);
void HYPKERNEL64 hk_mem_set(void *src, int8_t const val, uint64_t size);
void HYPKERNEL64 hk_write_pt_entry(uint8_t *const base, pt_entry_t const *const p_entry);
void HYPKERNEL64 hk_write_pd_entry(uint8_t *const base, pd_entry_t const *const p_entry);
void HYPKERNEL64 hk_write_pdpt_entry(uint8_t *const base, pdpt_entry_t const *const p_entry);
void HYPKERNEL64 hk_write_pml4_entry(uint8_t *const base, pml4_entry_t const *const p_entry);
#endif

223
x64/src/c/multiboot.h Normal file
View File

@ -0,0 +1,223 @@
/* multiboot.h - Multiboot header file. */
/* Copyright (C) 1999,2003,2007,2008,2009 Free Software Foundation, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY
* DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef MULTIBOOT_HEADER
#define MULTIBOOT_HEADER 1
/* How many bytes from the start of the file we search for the header. */
#define MULTIBOOT_SEARCH 8192
/* The magic field should contain this. */
#define MULTIBOOT_HEADER_MAGIC 0x1BADB002
/* This should be in %eax. */
#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002
/* The bits in the required part of flags field we don't support. */
#define MULTIBOOT_UNSUPPORTED 0x0000fffc
/* Alignment of multiboot modules. */
#define MULTIBOOT_MOD_ALIGN 0x00001000
/* Alignment of the multiboot info structure. */
#define MULTIBOOT_INFO_ALIGN 0x00000004
/* Flags set in the 'flags' member of the multiboot header. */
/* Align all boot modules on i386 page (4KB) boundaries. */
#define MULTIBOOT_PAGE_ALIGN 0x00000001
/* Must pass memory information to OS. */
#define MULTIBOOT_MEMORY_INFO 0x00000002
/* Must pass video information to OS. */
#define MULTIBOOT_VIDEO_MODE 0x00000004
/* This flag indicates the use of the address fields in the header. */
#define MULTIBOOT_AOUT_KLUDGE 0x00010000
/* Flags to be set in the 'flags' member of the multiboot info structure. */
/* is there basic lower/upper memory information? */
#define MULTIBOOT_INFO_MEMORY 0x00000001
/* is there a boot device set? */
#define MULTIBOOT_INFO_BOOTDEV 0x00000002
/* is the command-line defined? */
#define MULTIBOOT_INFO_CMDLINE 0x00000004
/* are there modules to do something with? */
#define MULTIBOOT_INFO_MODS 0x00000008
/* These next two are mutually exclusive */
/* is there a symbol table loaded? */
#define MULTIBOOT_INFO_AOUT_SYMS 0x00000010
/* is there an ELF section header table? */
#define MULTIBOOT_INFO_ELF_SHDR 0X00000020
/* is there a full memory map? */
#define MULTIBOOT_INFO_MEM_MAP 0x00000040
/* Is there drive info? */
#define MULTIBOOT_INFO_DRIVE_INFO 0x00000080
/* Is there a config table? */
#define MULTIBOOT_INFO_CONFIG_TABLE 0x00000100
/* Is there a boot loader name? */
#define MULTIBOOT_INFO_BOOT_LOADER_NAME 0x00000200
/* Is there a APM table? */
#define MULTIBOOT_INFO_APM_TABLE 0x00000400
/* Is there video information? */
#define MULTIBOOT_INFO_VIDEO_INFO 0x00000800
#ifndef ASM_FILE
typedef unsigned short multiboot_uint16_t;
typedef unsigned int multiboot_uint32_t;
typedef unsigned long long multiboot_uint64_t;
struct multiboot_header
{
/* Must be MULTIBOOT_MAGIC - see above. */
multiboot_uint32_t magic;
/* Feature flags. */
multiboot_uint32_t flags;
/* The above fields plus this one must equal 0 mod 2^32. */
multiboot_uint32_t checksum;
/* These are only valid if MULTIBOOT_AOUT_KLUDGE is set. */
multiboot_uint32_t header_addr;
multiboot_uint32_t load_addr;
multiboot_uint32_t load_end_addr;
multiboot_uint32_t bss_end_addr;
multiboot_uint32_t entry_addr;
/* These are only valid if MULTIBOOT_VIDEO_MODE is set. */
multiboot_uint32_t mode_type;
multiboot_uint32_t width;
multiboot_uint32_t height;
multiboot_uint32_t depth;
};
/* The symbol table for a.out. */
struct multiboot_aout_symbol_table
{
multiboot_uint32_t tabsize;
multiboot_uint32_t strsize;
multiboot_uint32_t addr;
multiboot_uint32_t reserved;
};
typedef struct multiboot_aout_symbol_table multiboot_aout_symbol_table_t;
/* The section header table for ELF. */
struct multiboot_elf_section_header_table
{
multiboot_uint32_t num;
multiboot_uint32_t size;
multiboot_uint32_t addr;
multiboot_uint32_t shndx;
};
typedef struct multiboot_elf_section_header_table multiboot_elf_section_header_table_t;
struct multiboot_info
{
/* Multiboot info version number */
multiboot_uint32_t flags;
/* Available memory from BIOS */
multiboot_uint32_t mem_lower;
multiboot_uint32_t mem_upper;
/* "root" partition */
multiboot_uint32_t boot_device;
/* Kernel command line */
multiboot_uint32_t cmdline;
/* Boot-Module list */
multiboot_uint32_t mods_count;
multiboot_uint32_t mods_addr;
union
{
multiboot_aout_symbol_table_t aout_sym;
multiboot_elf_section_header_table_t elf_sec;
} u;
/* Memory Mapping buffer */
multiboot_uint32_t mmap_length;
multiboot_uint32_t mmap_addr;
/* Drive Info buffer */
multiboot_uint32_t drives_length;
multiboot_uint32_t drives_addr;
/* ROM configuration table */
multiboot_uint32_t config_table;
/* Boot Loader Name */
multiboot_uint32_t boot_loader_name;
/* APM table */
multiboot_uint32_t apm_table;
/* Video */
multiboot_uint32_t vbe_control_info;
multiboot_uint32_t vbe_mode_info;
multiboot_uint16_t vbe_mode;
multiboot_uint16_t vbe_interface_seg;
multiboot_uint16_t vbe_interface_off;
multiboot_uint16_t vbe_interface_len;
};
typedef struct multiboot_info multiboot_info_t;
struct multiboot_mmap_entry
{
multiboot_uint32_t size;
multiboot_uint64_t addr;
multiboot_uint64_t len;
#define MULTIBOOT_MEMORY_AVAILABLE 1
#define MULTIBOOT_MEMORY_RESERVED 2
multiboot_uint32_t type;
} __attribute__((packed));
typedef struct multiboot_mmap_entry multiboot_memory_map_t;
struct multiboot_mod_list
{
/* the memory used goes from bytes 'mod_start' to 'mod_end-1' inclusive */
multiboot_uint32_t mod_start;
multiboot_uint32_t mod_end;
/* Module command line */
multiboot_uint32_t cmdline;
/* padding to take it to 16 bytes (must be zero) */
multiboot_uint32_t pad;
};
typedef struct multiboot_mod_list multiboot_module_t;
#endif /* ! ASM_FILE */
#endif /* ! MULTIBOOT_HEADER */

136
x64/src/c/print.c Normal file
View File

@ -0,0 +1,136 @@
#include "kdef.h"
#include "type.h"
#include "mem.h"
#include "print.h"
uint64_t text_pos;
uint64_t HYPKERNEL64 hk_str_len(char const * str)
{
uint32_t length = 0;
if(str == NULL)
return 0;
while(*str != 0)
{
str++;
length++;
}
return length;
}
uint64_t HYPKERNEL64 hk_str_cmp(char const * str1,char const * str2)
{
if(str1 == NULL || str2 == NULL)
return 0;
uint32_t length = hk_str_len(str1);
if(length != hk_str_len(str2))
return 0;
while(length--)
{
if(*(str1+length) != *(str2+length))
return 0;
}
return 1;
}
void HYPKERNEL64 hk_print_scroll()
{
hk_mem_move((void*)(0xb8000 + get_pos(1,0) * 2), (void*)(0xb8000 + get_pos(0,0) * 2), (80*24)*2);
return;
}
void HYPKERNEL64 hk_print_str(char const *str)
{
if(str == NULL)
return;
while (*str != 0)
{
if(*str == '\n')
{
text_pos = 80 * (get_row(text_pos) + 1);
if(text_pos > 80 * 25 - 1)
{
//can't hold
hk_print_scroll();
hk_mem_set((void*)(0xb8000 + 80*24*2), 0, 80 * 2); // clear last row
text_pos = 80 * 24;
}
str++;
}
else
{
if (text_pos > 80 * 25 - 1)
{
//can't hold
hk_print_scroll();
text_pos = 80 * 24;
}
*((char*)(0xb8000) + text_pos*2) = *str;
*((char*)(0xb8000) + text_pos*2 + 1) = 7;
str++;
text_pos++;
}
}
return;
}
void HYPKERNEL64 hk_print_int(int64_t number)
{
char arr[21]; // do not need to initialize
arr[20] = 0; //zero-terminated
uint32_t index = 19;
uint32_t isNegative = 0;
uint32_t const div = 10;
if (number < 0)
{
isNegative = 1;
number *= -1;
}
while (1)
{
int64_t quo = number / div;
int64_t rmd = number % div;
number = quo;
arr[index] = (char) ('0' + rmd);
index--;
if (number == 0)
break;
}
if (isNegative)
{
arr[index] = '-';
hk_print_str(&(arr[index]));
}
else
hk_print_str(&(arr[index+1]));
return;
}
void HYPKERNEL64 hk_print_hex(uint64_t number)
{
const char lookup_table[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char arr[19];
arr[18] = 0; //zero-terminated
uint32_t index = 17;
uint32_t const div = 16;
while (1)
{
uint64_t quo = number / div;
uint64_t rmd = number % div;
number = quo;
arr[index--] = lookup_table[rmd];
if (number == 0)
break;
}
arr[index--] = 'x';
arr[index] = '0';
hk_print_str(&(arr[index]));
return;
}
void HYPKERNEL64 hk_clear_screen(void)
{
text_pos = 0; // reset text_pos
hk_mem_set((void*)0xb8000, 0, 25*80*2);
return;
}

17
x64/src/c/print.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef _PRINT_H_
#define _PRINT_H_
#include "type.h"
#include "kdef.h"
#define get_column(pos) (pos % 80)
#define get_row(pos) (pos / 80)
#define get_pos(row,col) ((row) * 80 + (col))
void HYPKERNEL64 hk_print_hex(uint64_t number);
void HYPKERNEL64 hk_print_int(int64_t number);
void HYPKERNEL64 hk_print_str(char const *str);
void HYPKERNEL64 hk_clear_screen(void);
uint64_t HYPKERNEL64 hk_str_len(char const * str);
uint64_t HYPKERNEL64 hk_str_cmp(char const * str1,char const * str2);
#endif

View File

@ -8,8 +8,4 @@ typedef int int32_t;
typedef short int16_t;
typedef long long int64_t;
typedef char int8_t;
typedef uint8_t byte;
typedef uint16_t word;
typedef uint32_t dword;
typedef uint64_t qword;
#endif

View File

@ -2,10 +2,6 @@ global hk_load_gdt
global hk_support_x64
global hk_disable_paging
global hk_enable_paging
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, uint16 SLCT_CODE, uint16 SLCT_DATA)

View File

@ -1,6 +1,6 @@
global kernel_stack
global kernel_addr
global hk_comp
global hk_entry_comp
extern hk_main
extern hk_print_str
extern hk_print_hex
@ -9,7 +9,7 @@ extern hk_enable_paging
extern hk_disable_paging
extern g_gdt_ptr_64
[SECTION .multiboot]
[SECTION .entry]
[BITS 32]
GRUB_MAGIC equ 0x2BADB002
MULTIBOOT_MAGIC_NUMBER equ 0x1BADB002
@ -24,16 +24,16 @@ dd MULTIBOOT_HEADER
dd MULTIBOOT_HEADER
dd 0
dd 0
dd hk_grub_main
dd hk_entry_32
times 4096 db 0
kernel_stack:
hk_grub_main:
hk_entry_32:
cli
cmp eax,GRUB_MAGIC
jmp hk_grub_main.loaded_by_grub
jmp hk_entry_32.loaded_by_grub
hlt
.loaded_by_grub:
@ -69,8 +69,8 @@ rdmsr ; Read from the model-specific register.
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 l
mov eax,0x100000
; let cr3 point at page table
mov eax,0x200000
mov cr3,eax
; enable paging, enter compatibility mode
@ -78,20 +78,4 @@ call hk_enable_paging
; enter x64
lgdt [g_gdt_ptr_64]
jmp 8:hk_entry_x64
[SECTION .text]
[BITS 64]
hk_entry_x64:
cli
mov ax,24
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov ss,ax
mov rax, 0x1F201F201F201F20
mov ecx, 500
rep movsq
hlt
jmp 8:0x100000

View File

@ -13,23 +13,9 @@ extern uint32_t text_pos;
extern int32_t * kernel_stack;
extern void hk_entry_comp(void);
void HYPKERNEL32 hk_delay(uint32_t i)
{
uint32_t j = 100000;
while(i--)
{
while(j > 0)
{
j--;
}
}
return;
}
void HYPKERNEL32 hk_init_x64()
{
//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};
@ -56,12 +42,12 @@ void HYPKERNEL32 hk_init_x64()
//Setup identity x64 PAE paging
hk_print_str("*Setting up x86_64 paging...");
hk_print_str("*Setting up paging for x64...");
//Setup
//TODO: check for available memory and only allocate stuff needed(should not be really hard)
uint32_t addr = 0x100000;
pml4_entry_t pml4_entry = {.base = 0x101000,.Acc = 0, .PWT = 1, .PCD = 0,.Pr = 1,.USU = 0, .XD = 0, .RW = 1, .Sz = 0};
while(addr < 0x101000)
uint32_t addr = 0x200000;
pml4_entry_t pml4_entry = {.base = 0x201000,.Acc = 0, .PWT = 1, .PCD = 0,.Pr = 1,.USU = 0, .XD = 0, .RW = 1, .Sz = 0};
while(addr < 0x201000)
{
hk_write_pml4_entry((uint8_t *) addr,&pml4_entry);
//only map first 512 gigs for obvious reasons.
@ -69,9 +55,9 @@ void HYPKERNEL32 hk_init_x64()
pml4_entry.Pr = 0;
addr += 8;
}
addr = 0x101000;
pdpt_entry_t pdpt_entry = {.base = 0x102000,.Acc = 0, .PWT = 1, .PCD = 0,.Pr = 1,.USU = 0, .XD = 0, .RW = 1, .Sz = 0};
while(addr < 0x102000)
addr = 0x201000;
pdpt_entry_t pdpt_entry = {.base = 0x202000,.Acc = 0, .PWT = 1, .PCD = 0,.Pr = 1,.USU = 0, .XD = 0, .RW = 1, .Sz = 0};
while(addr < 0x202000)
{
hk_write_pdpt_entry((uint8_t*) addr, &pdpt_entry);
//only map first 1 gig for obvious reasons.
@ -79,18 +65,18 @@ void HYPKERNEL32 hk_init_x64()
pdpt_entry.base = 0;
addr += 8;
}
addr = 0x102000;
pd_entry_t pd_entry = {.base = 0x103000,.Acc = 0, .PWT = 1, .PCD = 0,.Pr = 1,.USU = 0, .XD = 0, .RW = 1, .Sz = 0};
while(addr < 0x103000)
addr = 0x202000;
pd_entry_t pd_entry = {.base = 0x203000,.Acc = 0, .PWT = 1, .PCD = 0,.Pr = 1,.USU = 0, .XD = 0, .RW = 1, .Sz = 0};
while(addr < 0x203000)
{
hk_write_pd_entry((uint8_t *) addr, &pd_entry);
//ah. no more laziness. At least we need to map first gig.
pd_entry.base += 0x1000; // increment for every 512 entries * 8 bytes each
addr += 8;
}
addr = 0x103000;
addr = 0x203000;
pt_entry_t pt_entry = {.base = 0, .Acc = 0, .Pr = 1, .RW = 1, .USU = 0, .PWT = 1,.PCD = 0, .dirty = 0,.Gl = 0, .PAT = 0,.XD = 0};
while(addr < (0x103000 + 512 * 0x1000))
while(addr < (0x203000 + 512 * 0x1000))
{
hk_write_pt_entry((uint8_t *) addr, &pt_entry);
pt_entry.base += 0x1000;

View File

@ -125,3 +125,11 @@ void HYPKERNEL32 hk_print_hex(uint32_t number)
hk_print_str(&(arr[index]));
return;
};
void HYPKERNEL32 hk_clear_screen(void)
{
text_pos = 0; // reset text_pos
hk_mem_set((void*)0xb8000, 0, 25*80*2);
return;
}

View File

@ -12,5 +12,5 @@ void HYPKERNEL32 hk_print_int(int32_t number);
void HYPKERNEL32 hk_print_str(char const *str);
uint32_t HYPKERNEL32 hk_str_len(char const * str);
uint32_t HYPKERNEL32 hk_str_cmp(char const * str1,char const * str2);
void HYPKERNEL32 hk_clear_screen(void);
#endif

View File

@ -1,132 +0,0 @@
#include "util.h"
#include "type32.h"
#include "kdef32.h"
int32 HYPKERNEL32 hk_set_bit(void* dst,uint32 bit)
{
if (dst == NULL)
return -1;
int32 quo = bit/32;
bit = bit%32;
int32* cDst = (int32*)dst + quo;
*(int32*)(cDst) |= 1 << bit;
return 0;
}
int32 HYPKERNEL32 hk_clear_bit(void* dst, uint32 bit)
{
if (dst == NULL)
return -1;
int32 quo = bit / 32;
bit = bit % 32;
int32* cDst = (int32*)dst + quo;
*(int32*)(cDst) &= ~(1 << bit);
return 0;
}
int32 HYPKERNEL32 hk_get_bit(void* dst, uint32 bit)
{
if (dst == NULL)
return -1;
int32 quo = bit / 32;
bit = bit % 32;
int32* cDst = (int32*)dst + quo;
return *(int32*)(cDst) & (1 << bit);
}
int32 HYPKERNEL32 hk_toggle_bit(void* dst, uint32 bit)
{
if (dst == NULL)
return -1;
int32 quo = bit / 32;
bit = bit % 32;
int32* cDst = (int32*)dst + quo;
*(int32*)(cDst) |= 1 << bit;
return 0;
}
int32 HYPKERNEL32 hk_memcpy(void* src, void* dst, uint32 size)
{
if (src == NULL || dst == NULL)
return -1;
char* cSrc = (char*)src;
char* cDst = (char*)dst;
while (size--)
*(cDst++) = *(cSrc++);
return 0;
}
int32 HYPKERNEL32 hk_memmove(void* src, void* dst, uint32 size)
{
if (src == NULL || dst == NULL)
return -1;
char* cSrc = (char*)src;
char* cDst = (char*)dst;
if (cSrc >= cDst)
{
return hk_memcpy(src,dst,size);
}
cSrc += size;
cDst += size;
while (size--)
*(--cDst) = *(--cSrc);
return 0;
}
int32 HYPKERNEL32 hk_print_string(char* str)
{
char* gs = (char*)(0xb8000);
uint8 attr = 0x07;
while (*(str) != 0)
{
*(gs) = *(str);
str++;
*(gs + 1) = attr;
gs += 2;
}
return 0;
}
int32 hk_print_int(int32 number)
{
char arr[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int32 index = 10;
int32 isNegative = 0;
int32 div = 10;
if (number < 0)
{
isNegative = 1;
number *= -1;
}
while (1)
{
int32 quo = number / div;
int32 rmd = number%div;
number = quo;
arr[index] = '0' + rmd;
index--;
if (number == 0)
break;
}
if (isNegative)
{
arr[index] = '-';
}
hk_print_string(&(arr[index]));
return 0;
}
int32 HkUpdateCursor(int32 row, int32 col)
{
if (row < 0 || col < 0 || row > 25 || col > 80)
return -1;
uint16 position = (row * 80) + col;
// cursor LOW port to vga INDEX register
hk_write_port(0x3D4, 0x0F);
hk_write_port(0x3D5, (uint8)(position & 0xFF));
// cursor HIGH port to vga INDEX register
hk_write_port(0x3D4, 0x0E);
hk_write_port(0x3D5, (uint8)((position >> 8) & 0xFF));
return 0;
}