[RUNNING] Structural and boot change:

1. Changed to multiboot2 complaint
2. Separated modules (hal, kernel, test, common)
3. Rewrite makefile/linker script/ grub.cfg to adapt changes.
This commit is contained in:
secXsQuared 2016-06-04 02:41:56 -07:00
parent 2475039543
commit e53e396556
54 changed files with 717 additions and 384 deletions

View File

@ -1,12 +0,0 @@
menuentry "HOS x86 [Dev]" {
multiboot /HOS/kernel32
}
menuentry "HOS x64 [Dev]" {
multiboot /HOS/kernel64
}
menuentry "HOS Multiboot2 [Pending]" {
insmod kernel64
multiboot2 /HOS/multiboot2.bin
}

23
x64/CMakeLists.txt Normal file
View File

@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 2.8.4)
project(Workspace)
MACRO(HEADER_DIRECTORIES return_list)
FILE(GLOB_RECURSE new_list ./*.h)
SET(dir_list "")
FOREACH(file_path ${new_list})
GET_FILENAME_COMPONENT(dir_path ${file_path} PATH)
SET(dir_list ${dir_list} ${dir_path})
ENDFOREACH()
LIST(REMOVE_DUPLICATES dir_list)
SET(${return_list} ${dir_list})
ENDMACRO()
HEADER_DIRECTORIES(header_dirs)
include_directories(${header_dirs})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
file(GLOB_RECURSE SOURCE_FILES ./*.h ./*.c)
add_executable(Workspace ${SOURCE_FILES})

55
x64/bochsUbuntu.bxrc Normal file
View File

@ -0,0 +1,55 @@
# configuration file generated by Bochs
plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, gameport=1, pci_ide=1, acpi=1, ioapic=1
config_interface: textconfig
display_library: sdl
magic_break: enabled=1
memory: host=128, guest=128
romimage: file="/usr/share/bochs/BIOS-bochs-latest"
vgaromimage: file="/usr/share/bochs/VGABIOS-lgpl-latest"
boot: cdrom
floppy_bootsig_check: disabled=0
# no floppya
# no floppyb
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
ata0-master: type=cdrom, path="secX.iso", status=inserted, biosdetect=auto, model="HOS"
ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15
ata2: enabled=0
ata3: enabled=0
parport1: enabled=1, file=""
parport2: enabled=0
com1: enabled=1, mode=null, dev=""
com2: enabled=0
com3: enabled=0
com4: enabled=0
usb_uhci: enabled=0
usb_ohci: enabled=0
i440fxsupport: enabled=1
vga_update_interval: 50000
vga: extension=vbe
cpu: count=1, ips=4000000, reset_on_triple_fault=1, ignore_bad_msrs=1
cpuid: cpuid_limit_winnt=0, mmx=1, sse=sse2, xapic=1, sep=1, aes=0, xsave=0, movbe=0, 1g_pages=1, pcid=0 fsgsbase=0
cpuid: stepping=3, vendor_string="GenuineIntel", brand_string=" Intel(R) Pentium(R) 4 CPU "
print_timestamps: enabled=0
# no gdb stub
port_e9_hack: enabled=0
text_snapshot_check: enabled=0
private_colormap: enabled=0
clock: sync=none, time0=local
# no cmosimage
ne2k: enabled=0
pnic: enabled=0
sb16: enabled=0
# no loader
log: -
logprefix: %t%e%d
panic: action=ask
error: action=report
info: action=report
debug: action=ignore
pass: action=fatal
keyboard_type: mf
keyboard_serial_delay: 250
keyboard_paste_delay: 100000
keyboard_mapping: enabled=0, map=
user_shortcut: keys=none
mouse: enabled=0, type=ps2, toggle=ctrl+mbutton

3
x64/grub.cfg Normal file
View File

@ -0,0 +1,3 @@
menuentry "secX x64 [Dev]" {
multiboot2 /secX/kernel
}

64
x64/makefile Normal file
View File

@ -0,0 +1,64 @@
ASM = nasm
CC = gcc
LD = ld
#Recursive Wildcard
rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
rdircard=$(sort $(dir $(call rwildcard,$1,*)))
rdircardex=$(sort $(dir $(call rwildcard,$1,$2)))
#x64 vars
LD_SCRIPT := linker.ld
GRUB_CFG := grub.cfg
OUTPUT_DIR := out
HEADER_DIRS := $(call rdircardex, *,*.h)
ALL_OUTPUT_DIRS := $(addprefix $(OUTPUT_DIR)/,$(call rdircard,*))
C_FLAGS := -m64 -std=c11 -g -c $(addprefix -I, $(HEADER_DIRS)) -fno-stack-protector -fno-builtin -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -masm=intel -Wall -Wextra -Wno-comment
ASM_FLAGS := -f elf64 -I $(ASM_SRC_PATH_64)/
LD_FLAGS := -melf_x86_64
C_SRC := $(call rwildcard, ./, *.c)
ASM_SRC := $(call rwildcard, ./, *.asm)
KERNEL_BIN := kernel.bin
KERNEL_ELF := kernel.elf
#Object files
C_OBJ := $(C_SRC:.c=.o)
ASM_OBJ := $(ASM_SRC:.asm=.oasm)
ALL_OBJ := $(addprefix $(OUTPUT_DIR)/,$(C_OBJ)) $(addprefix $(OUTPUT_DIR)/,$(ASM_OBJ))
#Commands
all: init compile link buildiso clean
init:
mkdir -p $(ALL_OUTPUT_DIRS)
compile: $(C_OBJ) $(ASM_OBJ)
link: $(KERNEL_BIN)
buildiso:
mkdir -p $(OUTPUT_DIR)/temp_iso/secX
mkdir -p $(OUTPUT_DIR)/temp_iso/boot
mkdir -p $(OUTPUT_DIR)/temp_iso/boot/grub
mv $(OUTPUT_DIR)/$(KERNEL_BIN) $(OUTPUT_DIR)/temp_iso/secX/kernel
cp $(GRUB_CFG) $(OUTPUT_DIR)/temp_iso/boot/grub/
grub-mkrescue -o secX.iso $(OUTPUT_DIR)/temp_iso
rm -rf $(OUTPUT_DIR)/temp_iso
clean:
rm -rf $(OUTPUT_DIR)
%.o: %.c
$(CC) $(C_FLAGS) -o $(OUTPUT_DIR)/$@ $^
%.oasm: %.asm
$(ASM) $(ASM_FLAGS) -o $(OUTPUT_DIR)/$@ $^
$(KERNEL_BIN): $(ALL_OBJ)
$(LD) $(LD_FLAGS) -T $(LD_SCRIPT) -o $(OUTPUT_DIR)/$(KERNEL_BIN) $(ALL_OBJ)

View File

@ -1,223 +0,0 @@
/* 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 */

View File

@ -1,17 +0,0 @@
#include "k_intr.h"
#include "k_hal.h"
void k_set_interrupt_handler(uint64_t index, void (*handler)(void))
{
hal_set_interrupt_handler(index, handler);
}
void k_disable_interrupt()
{
}
void k_enable_interrupt()
{
}

View File

@ -3,7 +3,7 @@
* See COPYING under root for details
*/
#include "inc/linked_list.h"
#include "linked_list.h"
static void KAPI _init_linked_list_node(linked_list_node_t *node)
{

View File

@ -2,9 +2,9 @@
; Distributed under GPL license
; See COPYING under root for details
global hal_interlocked_exchange;
;============================
;uint64_t _KERNEL_ABI hal_interlocked_exchange(_IN _OUT uint64_t* dst, _IN uint64_t val);
global hal_interlocked_exchange;
hal_interlocked_exchange:
lock xchg qword [rdi], rsi
mov rax, rsi

View File

@ -3,39 +3,62 @@
; See COPYING under root for details
extern kmain
extern kernel_start
extern kernel_end
global BOCHS_MAGIC_BREAKPOINT
; IMPORTANT: This module should be 4k-page aliened
[SECTION .entry]
[BITS 32]
; MultiBoot Header
GRUB_MAGIC equ 0x2BADB002
MULTIBOOT_MAGIC_NUMBER equ 0x1BADB002
MULTIBOOT_FLAGS equ 0x10003
MULTIBOOT_CHECK_SUM equ - (MULTIBOOT_MAGIC_NUMBER + MULTIBOOT_FLAGS)
MULTIBOOT_HEADER:
align 4
dd MULTIBOOT_MAGIC_NUMBER
dd MULTIBOOT_FLAGS
dd MULTIBOOT_CHECK_SUM
MULTIBOOT_LOADED_MAGIC equ 0x36d76289
MULTIBOOT_MAGIC_NUMBER equ 0xE85250D6
MULTIBOOT_ARCH equ 0
MULTIBOOT_CHECK_SUM equ - (MULTIBOOT_MAGIC_NUMBER + MULTIBOOT_HEADER_SIZE + MULTIBOOT_ARCH)
dd MULTIBOOT_HEADER
dd MULTIBOOT_HEADER
dd 0
dd 0
align 8
MULTIBOOT_HEADER:
dd MULTIBOOT_MAGIC_NUMBER
dd MULTIBOOT_ARCH
dd MULTIBOOT_HEADER_SIZE
dd MULTIBOOT_CHECK_SUM
;====================
;Address_tag
MULTIBOOT_ADDRESS_TAG:
dw 0x2 ;type=2
dw 0x0 ;flag=0
dd MULTIBOOT_ADDRESS_TAG_SIZE; size
dd MULTIBOOT_HEADER ; Since at the beginning of the file
dd MULTIBOOT_HEADER ; load start
dd 0 ; load end
dd 0 ; bss
MULTIBOOT_ADDRESS_TAG_SIZE equ ( $ - MULTIBOOT_ADDRESS_TAG)
;====================
;Entry_tag
align 8
MULTIBOOT_ENTRY_TAG:
dw 0x3; type=3
dw 0x0; flag=0
dd MULTIBOOT_ENTRY_TAG_SIZE
dd entry_32
MULTIBOOT_ENTRY_TAG_SIZE equ ($ - MULTIBOOT_ENTRY_TAG)
;====================
;End_tag
align 8
dw 0x0
dw 0x0
dd 0x8
;====================
MULTIBOOT_HEADER_SIZE equ ($ - MULTIBOOT_HEADER)
align 4096 ;4k alignment
align 4096
; temporary page table
PML4_BASE:
times 512 dq 0 ;reserved the rest for page entries
align 4096 ;4k alignment
align 4096
PDPT_BASE:
times 512 dq 0 ;reserved the rest for page entries
align 4096
; long mode gdt
GDT64: ; Global Descriptor Table (64-bit).
; NULL
@ -63,12 +86,14 @@ GDT64: ; Global Descriptor Table (64-bit).
dw $ - GDT64 - 1 ; Limit.
dq GDT64 ; Base.
align 4096
entry_32:
; close interrupt
cli
cld
; check loaded by grub
cmp eax,GRUB_MAGIC
cmp eax,MULTIBOOT_LOADED_MAGIC
je .loaded_by_grub
hlt
.loaded_by_grub:
@ -188,10 +213,6 @@ mov rdi,rsi ; multiboot_info*
call kmain
hlt
BOCHS_MAGIC_BREAKPOINT:
xchg bx,bx
ret
align 4096 ;4k alignment
times 8192 db 0
KERNEL_STACK:

View File

@ -13,9 +13,9 @@
boot_info_t *KAPI hal_init(void *_m_info)
{
multiboot_info_t* m_info = (multiboot_info_t*) _m_info;
if (m_info == NULL)
return NULL;
//multiboot_info_t* m_info = (multiboot_info_t*) _m_info;
// if (m_info == NULL)
// return NULL;
// set up kernel heap;
hal_alloc_init();
@ -58,39 +58,39 @@ boot_info_t *KAPI hal_init(void *_m_info)
mem_set(boot_info, 0, sizeof(boot_info_t));
// obtain boot information
// memory info
if (m_info->flags & (1 << 6))
{
boot_info->mem_info = (mem_info_t *) halloc(sizeof(mem_info_t));
hal_assert(boot_info->mem_info != NULL, "Unable to allocate memory for mem_info.");
boot_info->mem_info->mem_available = 0;
boot_info->mem_info->mem_installed = 0;
boot_info->mem_info->free_page_list = (linked_list_t *) halloc((sizeof(linked_list_t)));
boot_info->mem_info->occupied_page_list = (linked_list_t *) halloc((sizeof(linked_list_t)));
hal_assert(boot_info->mem_info->free_page_list != NULL &&
boot_info->mem_info->occupied_page_list != NULL, "Unable to allocate memory for mem_info_lists.");
linked_list_init(boot_info->mem_info->free_page_list);
linked_list_init(boot_info->mem_info->occupied_page_list);
multiboot_memory_map_t *mem_map = (multiboot_memory_map_t *) (uint64_t)m_info->mmap_addr;
uint64_t mem_map_size = m_info->mmap_length / sizeof(multiboot_memory_map_t);
for (uint64_t i = 0; i < mem_map_size; i++)
{
hal_printf("==Base: 0x%X, Length: %u, Type: %s==\n", (mem_map + i)->addr, (mem_map + i)->len,
(mem_map + i)->type == MULTIBOOT_MEMORY_AVAILABLE ? "AVL" : "RSV");
if ((mem_map + i)->type == MULTIBOOT_MEMORY_AVAILABLE)
{
uint64_t base_addr = (mem_map + i)->addr;
uint64_t end_addr = base_addr + (mem_map + i)->len;
// align head
uint64_t aligned_base_addr = align_up(base_addr, PHYSICAL_PAGE_SIZE);
// align tail
uint64_t aligned_end_addr = align_down(end_addr, PHYSICAL_PAGE_SIZE);
uint64_t page_count = (aligned_end_addr - aligned_base_addr) / PHYSICAL_PAGE_SIZE;
if (page_count == 0)
continue;
// if (m_info->flags & (1 << 6))
// {
// boot_info->mem_info = (mem_info_t *) halloc(sizeof(mem_info_t));
// hal_assert(boot_info->mem_info != NULL, "Unable to allocate memory for mem_info.");
// boot_info->mem_info->mem_available = 0;
// boot_info->mem_info->mem_installed = 0;
// boot_info->mem_info->free_page_list = (linked_list_t *) halloc((sizeof(linked_list_t)));
// boot_info->mem_info->occupied_page_list = (linked_list_t *) halloc((sizeof(linked_list_t)));
// hal_assert(boot_info->mem_info->free_page_list != NULL &&
// boot_info->mem_info->occupied_page_list != NULL, "Unable to allocate memory for mem_info_lists.");
// linked_list_init(boot_info->mem_info->free_page_list);
// linked_list_init(boot_info->mem_info->occupied_page_list);
// multiboot_memory_map_t *mem_map = (multiboot_memory_map_t *) (uint64_t)m_info->mmap_addr;
// uint64_t mem_map_size = m_info->mmap_length / sizeof(multiboot_memory_map_t);
// for (uint64_t i = 0; i < mem_map_size; i++)
// {
// hal_printf("==Base: 0x%X, Length: %u, Type: %s==\n", (mem_map + i)->addr, (mem_map + i)->len,
// (mem_map + i)->type == MULTIBOOT_MEMORY_AVAILABLE ? "AVL" : "RSV");
// if ((mem_map + i)->type == MULTIBOOT_MEMORY_AVAILABLE)
// {
// uint64_t base_addr = (mem_map + i)->addr;
// uint64_t end_addr = base_addr + (mem_map + i)->len;
//
// // align head
// uint64_t aligned_base_addr = align_up(base_addr, PHYSICAL_PAGE_SIZE);
// // align tail
// uint64_t aligned_end_addr = align_down(end_addr, PHYSICAL_PAGE_SIZE);
//
//
// uint64_t page_count = (aligned_end_addr - aligned_base_addr) / PHYSICAL_PAGE_SIZE;
//
// if (page_count == 0)
// continue;
// strip kernel-occupied pages
// TODO: Finished this.
@ -119,49 +119,49 @@ boot_info_t *KAPI hal_init(void *_m_info)
// }
// }
memory_descriptor_node_t *each_desc = (memory_descriptor_node_t *) halloc(
sizeof(memory_descriptor_node_t));
hal_assert(each_desc != NULL, "Unable to allocate memory for memory_descriptor.");
each_desc->page_count = page_count;
each_desc->base_addr = aligned_base_addr;
linked_list_push_back(boot_info->mem_info->free_page_list, &each_desc->list_node);
boot_info->mem_info->mem_available += aligned_end_addr - aligned_base_addr;
}
boot_info->mem_info->mem_installed += (mem_map + i)->len;
}
}
else
{
// halt machine
hal_printf("HAL: Cannot detect memory information.");
hal_halt_cpu();
}
// loaded kernel modules
if (m_info->flags & (1 << 3))
{
boot_info->module_info = (module_info_t *) halloc(sizeof(module_info_t));
hal_assert(boot_info->module_info != NULL, "Unable to allocate memory for module_info.");
boot_info->module_info->module_count = 0;
boot_info->module_info->module_list = (linked_list_t *) halloc(sizeof(linked_list_t));
hal_assert(boot_info->module_info->module_list != NULL, "Unable to allocate memory for module_list.");
linked_list_init(boot_info->module_info->module_list);
multiboot_module_t *mods_list = (multiboot_module_t *) (uint64_t) m_info->mods_addr;
boot_info->module_info->module_count = m_info->mods_count;
for (uint64_t i = 0; i < boot_info->module_info->module_count; i++)
{
module_descriptor_node_t *each_module = (module_descriptor_node_t *) halloc(
sizeof(module_descriptor_node_t));
hal_assert(each_module != NULL, "Unable to allocate memory for module_descriptor.");
each_module->base_addr = (mods_list + i)->mod_start;
each_module->size = (mods_list + i)->mod_end - (mods_list + i)->mod_start;
each_module->name = (char *) halloc((size_t) str_len((char *) (uint64_t) (mods_list + i)->cmdline) + 1);
hal_assert(each_module->name != NULL, "Unable to allocate memory for module name string.");
mem_cpy((void *) (uint64_t) (mods_list + i)->cmdline, each_module->name,
str_len((char *) (uint64_t) (mods_list + i)->cmdline) + 1);
linked_list_push_back(boot_info->module_info->module_list, &each_module->list_node);
}
}
// memory_descriptor_node_t *each_desc = (memory_descriptor_node_t *) halloc(
// sizeof(memory_descriptor_node_t));
// hal_assert(each_desc != NULL, "Unable to allocate memory for memory_descriptor.");
// each_desc->page_count = page_count;
// each_desc->base_addr = aligned_base_addr;
// linked_list_push_back(boot_info->mem_info->free_page_list, &each_desc->list_node);
// boot_info->mem_info->mem_available += aligned_end_addr - aligned_base_addr;
// }
// boot_info->mem_info->mem_installed += (mem_map + i)->len;
// }
// }
// else
// {
// // halt machine
// hal_printf("HAL: Cannot detect memory information.");
// hal_halt_cpu();
// }
//
// // loaded kernel modules
// if (m_info->flags & (1 << 3))
// {
// boot_info->module_info = (module_info_t *) halloc(sizeof(module_info_t));
// hal_assert(boot_info->module_info != NULL, "Unable to allocate memory for module_info.");
// boot_info->module_info->module_count = 0;
// boot_info->module_info->module_list = (linked_list_t *) halloc(sizeof(linked_list_t));
// hal_assert(boot_info->module_info->module_list != NULL, "Unable to allocate memory for module_list.");
// linked_list_init(boot_info->module_info->module_list);
// multiboot_module_t *mods_list = (multiboot_module_t *) (uint64_t) m_info->mods_addr;
// boot_info->module_info->module_count = m_info->mods_count;
// for (uint64_t i = 0; i < boot_info->module_info->module_count; i++)
// {
// module_descriptor_node_t *each_module = (module_descriptor_node_t *) halloc(
// sizeof(module_descriptor_node_t));
// hal_assert(each_module != NULL, "Unable to allocate memory for module_descriptor.");
// each_module->base_addr = (mods_list + i)->mod_start;
// each_module->size = (mods_list + i)->mod_end - (mods_list + i)->mod_start;
// each_module->name = (char *) halloc((size_t) str_len((char *) (uint64_t) (mods_list + i)->cmdline) + 1);
// hal_assert(each_module->name != NULL, "Unable to allocate memory for module name string.");
// mem_cpy((void *) (uint64_t) (mods_list + i)->cmdline, each_module->name,
// str_len((char *) (uint64_t) (mods_list + i)->cmdline) + 1);
// linked_list_push_back(boot_info->module_info->module_list, &each_module->list_node);
// }
// }
// detect APIC
cpuid_t cpuid_info;

View File

@ -2,14 +2,6 @@
; Distributed under GPL license
; See COPYING under root for details
global hal_write_port
global hal_read_port
global hal_enable_interrupt
global hal_disable_interrupt
global hal_interrupt_handler_wrapper
global hal_halt_cpu
extern hal_interrupt_handler_dummy
%macro pushaq 0
push rax ;save current rax
push rbx ;save current rbx
@ -48,6 +40,8 @@ extern hal_interrupt_handler_dummy
[SECTION .text]
[BITS 64]
;====================
global hal_write_port
hal_write_port:
mov rdx,rdi
mov rax,rsi
@ -56,6 +50,8 @@ nop
nop
ret
;====================
global hal_read_port
hal_read_port:
mov rdx,rdi
xor rax,rax
@ -64,21 +60,36 @@ nop
nop
ret
;====================
global hal_disable_interrupt
hal_disable_interrupt:
cli
ret
;====================
global hal_enable_interrupt
hal_enable_interrupt:
sti
ret
hal_interrupt_handler_wrapper:
;====================
global hal_interrupt_handler
extern hal_interrupt_dispatcher
hal_interrupt_handler:
pushaq
cld
call hal_interrupt_handler_dummy
call hal_interrupt_dispatcher
popaq
iretq
;====================
global hal_halt_cpu
hal_halt_cpu:
hlt
ret
;====================
global hal_bochs_magic_breakpoint
hal_bochs_magic_breakpoint:
xchg bx,bx
ret

View File

@ -49,3 +49,9 @@ void KAPI hal_assert(int64_t expression,
}
return;
}
void KAPI hal_interrupt_dispatcher(void)
{
hal_printf("Yep... That just happened, an interrupt...\n");
return;
}

View File

@ -2,18 +2,14 @@
; Distributed under GPL license
; See COPYING under root for details
global hal_flush_gdt
global hal_flush_tlb
global hal_flush_idt
global hal_cpuid
global hal_read_page_base
global hal_write_page_base
;Functions preserve the registers rbx, rsp, rbp, r12, r13, r14, and 15
;rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11 are scratch registers.
;function parameter: rdi,rsi,rdx,rcx,r8,r9
[SECTION .text]
[BITS 64]
;======================
global hal_flush_gdt
hal_flush_gdt:
push rbp
mov rbp,rsp
@ -39,13 +35,16 @@ mov rsp,rbp
pop rbp
ret
;======================
global hal_flush_tlb
;void flush_tlb(void)
hal_flush_tlb:
mov rax,cr3
mov cr3,rax
ret
;======================
global hal_cpuid
;void get_cpuid(int64_t* rax, int64_t* rbx, int64_t* rcx, int64_t* rdx)
hal_cpuid:
mov rax,[rdi]
@ -59,19 +58,22 @@ pop rcx
mov [rcx],rdx
ret
;flush_idt
;======================
global hal_flush_idt
hal_flush_idt:
lidt [rdi]
ret
;read_cr3
;======================
global hal_read_page_base
hal_read_page_base:
mov rax,cr3
mov r11,0xFFFFFFFFFF000
and rax,r11 ;keep bits 12-51
ret
;write_cr3
;======================
global hal_write_page_base
hal_write_page_base:
mov r11,0xFFFFFFFFFF000
and rdi,r11 ;keep bits 12-51

View File

@ -19,7 +19,7 @@
extern void KAPI hal_write_port(uint64_t port, int64_t data);
extern int64_t KAPI hal_read_port(uint64_t port);
void KAPI hal_interrupt_handler_dummy();
void KAPI hal_interrupt_handler_dispatcher();
void KAPI hal_set_interrupt_handler(uint64_t index, void (*handler)(void));
extern void KAPI hal_enable_interrupt();

View File

@ -0,0 +1,375 @@
/* multiboot2.h - Multiboot 2 header file. */
/* Copyright (C) 1999,2003,2007,2008,2009,2010 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 32768
#define MULTIBOOT_HEADER_ALIGN 8
/* The magic field should contain this. */
#define MULTIBOOT2_HEADER_MAGIC 0xe85250d6
/* This should be in %eax. */
#define MULTIBOOT2_BOOTLOADER_MAGIC 0x36d76289
/* Alignment of multiboot modules. */
#define MULTIBOOT_MOD_ALIGN 0x00001000
/* Alignment of the multiboot info structure. */
#define MULTIBOOT_INFO_ALIGN 0x00000008
/* Flags set in the 'flags' member of the multiboot header. */
#define MULTIBOOT_TAG_ALIGN 8
#define MULTIBOOT_TAG_TYPE_END 0
#define MULTIBOOT_TAG_TYPE_CMDLINE 1
#define MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME 2
#define MULTIBOOT_TAG_TYPE_MODULE 3
#define MULTIBOOT_TAG_TYPE_BASIC_MEMINFO 4
#define MULTIBOOT_TAG_TYPE_BOOTDEV 5
#define MULTIBOOT_TAG_TYPE_MMAP 6
#define MULTIBOOT_TAG_TYPE_VBE 7
#define MULTIBOOT_TAG_TYPE_FRAMEBUFFER 8
#define MULTIBOOT_TAG_TYPE_ELF_SECTIONS 9
#define MULTIBOOT_TAG_TYPE_APM 10
#define MULTIBOOT_TAG_TYPE_EFI32 11
#define MULTIBOOT_TAG_TYPE_EFI64 12
#define MULTIBOOT_TAG_TYPE_SMBIOS 13
#define MULTIBOOT_TAG_TYPE_ACPI_OLD 14
#define MULTIBOOT_TAG_TYPE_ACPI_NEW 15
#define MULTIBOOT_TAG_TYPE_NETWORK 16
#define MULTIBOOT_TAG_TYPE_EFI_MMAP 17
#define MULTIBOOT_TAG_TYPE_EFI_BS 18
#define MULTIBOOT_HEADER_TAG_END 0
#define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST 1
#define MULTIBOOT_HEADER_TAG_ADDRESS 2
#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS 3
#define MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS 4
#define MULTIBOOT_HEADER_TAG_FRAMEBUFFER 5
#define MULTIBOOT_HEADER_TAG_MODULE_ALIGN 6
#define MULTIBOOT_HEADER_TAG_EFI_BS 7
#define MULTIBOOT_ARCHITECTURE_I386 0
#define MULTIBOOT_ARCHITECTURE_MIPS32 4
#define MULTIBOOT_HEADER_TAG_OPTIONAL 1
#define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
#define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
#ifndef ASM_FILE
typedef unsigned char multiboot_uint8_t;
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;
/* ISA */
multiboot_uint32_t architecture;
/* Total header length. */
multiboot_uint32_t header_length;
/* The above fields plus this one must equal 0 mod 2^32. */
multiboot_uint32_t checksum;
};
struct multiboot_header_tag
{
multiboot_uint16_t type;
multiboot_uint16_t flags;
multiboot_uint32_t size;
};
struct multiboot_header_tag_information_request
{
multiboot_uint16_t type;
multiboot_uint16_t flags;
multiboot_uint32_t size;
multiboot_uint32_t requests[0];
};
struct multiboot_header_tag_address
{
multiboot_uint16_t type;
multiboot_uint16_t flags;
multiboot_uint32_t size;
multiboot_uint32_t header_addr;
multiboot_uint32_t load_addr;
multiboot_uint32_t load_end_addr;
multiboot_uint32_t bss_end_addr;
};
struct multiboot_header_tag_entry_address
{
multiboot_uint16_t type;
multiboot_uint16_t flags;
multiboot_uint32_t size;
multiboot_uint32_t entry_addr;
};
struct multiboot_header_tag_console_flags
{
multiboot_uint16_t type;
multiboot_uint16_t flags;
multiboot_uint32_t size;
multiboot_uint32_t console_flags;
};
struct multiboot_header_tag_framebuffer
{
multiboot_uint16_t type;
multiboot_uint16_t flags;
multiboot_uint32_t size;
multiboot_uint32_t width;
multiboot_uint32_t height;
multiboot_uint32_t depth;
};
struct multiboot_header_tag_module_align
{
multiboot_uint16_t type;
multiboot_uint16_t flags;
multiboot_uint32_t size;
};
struct multiboot_color
{
multiboot_uint8_t red;
multiboot_uint8_t green;
multiboot_uint8_t blue;
};
struct multiboot_mmap_entry
{
multiboot_uint64_t addr;
multiboot_uint64_t len;
#define MULTIBOOT_MEMORY_AVAILABLE 1
#define MULTIBOOT_MEMORY_RESERVED 2
#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3
#define MULTIBOOT_MEMORY_NVS 4
#define MULTIBOOT_MEMORY_BADRAM 5
multiboot_uint32_t type;
multiboot_uint32_t zero;
} GRUB_PACKED;
typedef struct multiboot_mmap_entry multiboot_memory_map_t;
struct multiboot_tag
{
multiboot_uint32_t type;
multiboot_uint32_t size;
};
struct multiboot_tag_string
{
multiboot_uint32_t type;
multiboot_uint32_t size;
char string[0];
};
struct multiboot_tag_module
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint32_t mod_start;
multiboot_uint32_t mod_end;
char cmdline[0];
};
struct multiboot_tag_basic_meminfo
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint32_t mem_lower;
multiboot_uint32_t mem_upper;
};
struct multiboot_tag_bootdev
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint32_t biosdev;
multiboot_uint32_t slice;
multiboot_uint32_t part;
};
struct multiboot_tag_mmap
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint32_t entry_size;
multiboot_uint32_t entry_version;
struct multiboot_mmap_entry entries[0];
};
struct multiboot_vbe_info_block
{
multiboot_uint8_t external_specification[512];
};
struct multiboot_vbe_mode_info_block
{
multiboot_uint8_t external_specification[256];
};
struct multiboot_tag_vbe
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint16_t vbe_mode;
multiboot_uint16_t vbe_interface_seg;
multiboot_uint16_t vbe_interface_off;
multiboot_uint16_t vbe_interface_len;
struct multiboot_vbe_info_block vbe_control_info;
struct multiboot_vbe_mode_info_block vbe_mode_info;
};
struct multiboot_tag_framebuffer_common
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint64_t framebuffer_addr;
multiboot_uint32_t framebuffer_pitch;
multiboot_uint32_t framebuffer_width;
multiboot_uint32_t framebuffer_height;
multiboot_uint8_t framebuffer_bpp;
#define MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED 0
#define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1
#define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2
multiboot_uint8_t framebuffer_type;
multiboot_uint16_t reserved;
};
struct multiboot_tag_framebuffer
{
struct multiboot_tag_framebuffer_common common;
union
{
struct
{
multiboot_uint16_t framebuffer_palette_num_colors;
struct multiboot_color framebuffer_palette[0];
};
struct
{
multiboot_uint8_t framebuffer_red_field_position;
multiboot_uint8_t framebuffer_red_mask_size;
multiboot_uint8_t framebuffer_green_field_position;
multiboot_uint8_t framebuffer_green_mask_size;
multiboot_uint8_t framebuffer_blue_field_position;
multiboot_uint8_t framebuffer_blue_mask_size;
};
};
};
struct multiboot_tag_elf_sections
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint32_t num;
multiboot_uint32_t entsize;
multiboot_uint32_t shndx;
char sections[0];
};
struct multiboot_tag_apm
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint16_t version;
multiboot_uint16_t cseg;
multiboot_uint32_t offset;
multiboot_uint16_t cseg_16;
multiboot_uint16_t dseg;
multiboot_uint16_t flags;
multiboot_uint16_t cseg_len;
multiboot_uint16_t cseg_16_len;
multiboot_uint16_t dseg_len;
};
struct multiboot_tag_efi32
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint32_t pointer;
};
struct multiboot_tag_efi64
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint64_t pointer;
};
struct multiboot_tag_smbios
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint8_t major;
multiboot_uint8_t minor;
multiboot_uint8_t reserved[6];
multiboot_uint8_t tables[0];
};
struct multiboot_tag_old_acpi
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint8_t rsdp[0];
};
struct multiboot_tag_new_acpi
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint8_t rsdp[0];
};
struct multiboot_tag_network
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint8_t dhcpack[0];
};
struct multiboot_tag_efi_mmap
{
multiboot_uint32_t type;
multiboot_uint32_t size;
multiboot_uint32_t descr_size;
multiboot_uint32_t descr_vers;
multiboot_uint8_t efi_mmap[0];
};
#endif /* ! ASM_FILE */
#endif /* ! MULTIBOOT_HEADER */

View File

@ -37,6 +37,6 @@ extern void KAPI hal_halt_cpu(void);
// hal memory
//debug
extern void KAPI BOCHS_MAGIC_BREAKPOINT();
extern void KAPI hal_bochs_magic_breakpoint();
#endif

View File

@ -1,6 +1,7 @@
#ifndef _K_INTR_H_
#define _K_INTR_H_
#include "k_type.h"
#include "k_def.h"
typedef uint64_t irql_t;
@ -9,6 +10,11 @@ typedef uint64_t irql_t;
#define IRQL_KERNEL 3
#define IRQL_USER 4
void KAPI k_set_interrupt_handler(uint64_t index, void (*handler)(void));
void KAPI k_disable_interrupt();
void KAPI k_enable_interrupt();
#endif

View File

@ -18,6 +18,9 @@ void KAPI kmain(void *multiboot_info)
avl_tree_test();
salloc_test();
hal_printf("KRNL: Kernel task finished.");
hal_halt_cpu();
if(boot_info->mem_info != NULL)
{
hal_printf("Installed Memory: %uB\n", boot_info->mem_info->mem_installed);
@ -48,10 +51,10 @@ void KAPI kmain(void *multiboot_info)
}
// setup interrupt
for(uint64_t i = 0; i <= 21; i++)
{
hal_set_interrupt_handler(i, hal_interrupt_handler_wrapper);
}
// for(uint64_t i = 0; i <= 21; i++)
// {
// hal_set_interrupt_handler(i, hal_interrupt_handler_wrapper);
// }
// hal_enable_interrupt();
hal_printf("KRNL: Kernel task finished.");

17
x64/src/kernel/k_intr.c Normal file
View File

@ -0,0 +1,17 @@
#include "k_intr.h"
#include "k_hal.h"
void KAPI k_set_interrupt_handler(uint64_t index, void (*handler)(void))
{
hal_set_interrupt_handler(index, handler);
}
void KAPI k_disable_interrupt()
{
hal_disable_interrupt();
}
void KAPI k_enable_interrupt()
{
hal_disable_interrupt();
}

View File

@ -1,6 +1,5 @@
#include "hal_print.h"
#include "k_test_driver.h"
#include "k_type.h"
#include "hal_mem.h"
#define GAT_SIZE 256