Everything compiled again, with a lot of changes to the design of HAL.
Although a lot of things are not working yet. It's getting closer.
This commit is contained in:
parent
bfd0595926
commit
03d3d4ab70
|
@ -1,69 +1,67 @@
|
|||
#include <balloc.h>
|
||||
#include "linked_list.h"
|
||||
/* Copyright 2016 secXsQuared
|
||||
* Distributed under GPL license
|
||||
* See COPYING under root for details
|
||||
*/
|
||||
|
||||
#include "balloc.h"
|
||||
#include "std_lib.h"
|
||||
#include "bit_ops.h"
|
||||
|
||||
typedef struct
|
||||
int32_t balloc_init(balloc_desc_t *desc,
|
||||
uint64_t base,
|
||||
uint64_t end,
|
||||
uint64_t page_size,
|
||||
balloc_alloc_func alloc,
|
||||
balloc_free_func free,
|
||||
balloc_lock_func lock,
|
||||
balloc_unlock_func unlock)
|
||||
{
|
||||
linked_list_node_t list_node;
|
||||
} balloc_header_t;
|
||||
if(desc == NULL || base >= end || page_size == 0 || alloc == 0
|
||||
|| free == 0 || lock == 0 || unlock == 0)
|
||||
return BALLOC_STATUS_INVALID_ARGUMENTS;
|
||||
|
||||
static inline uint32_t _get_min_granularity()
|
||||
{
|
||||
return sizeof(balloc_header_t);
|
||||
}
|
||||
if(((end - base) % page_size) != 0)
|
||||
return BALLOC_STATUS_INVALID_ALIGNMENT;
|
||||
|
||||
static inline int32_t _order_of_two(uint32_t num)
|
||||
{
|
||||
int32_t result = -1;
|
||||
if(num == 1)
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
else if(num % 2 == 0 && num != 0)
|
||||
{
|
||||
result = 1;
|
||||
while((num >> result) != 1)
|
||||
{
|
||||
result++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
desc->alloc = alloc;
|
||||
desc->free = free;
|
||||
desc->lock = lock;
|
||||
desc->unlock = unlock;
|
||||
|
||||
static inline int32_t _get_max_order(uint32_t size, uint32_t granularity)
|
||||
{
|
||||
if(size == 0 || granularity == 0 || granularity > size || size % granularity != 0)
|
||||
return -1;
|
||||
return _order_of_two(size/granularity);
|
||||
}
|
||||
|
||||
int32_t balloc_free_list_size(uint32_t size, uint32_t granularity)
|
||||
{
|
||||
int32_t result = _get_max_order(size,granularity);
|
||||
return result == -1 ? (result) : (result) * ((int32_t)sizeof(linked_list_t));
|
||||
}
|
||||
|
||||
int32_t balloc_bit_map_size(uint32_t size, uint32_t granularity)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int32_t balloc_init(balloc_desc_t* desc,
|
||||
void* base,
|
||||
uint32_t size,
|
||||
uint32_t granularity)
|
||||
{
|
||||
if( desc == NULL || base == NULL || granularity == 0 || size == 0 ||
|
||||
granularity < _get_min_granularity() ||
|
||||
size < granularity ||
|
||||
size % granularity != 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
desc->size = size;
|
||||
desc->base = base;
|
||||
desc->granularity = granularity;
|
||||
desc->bit_map = bit_map;
|
||||
desc->free_list = free_list;
|
||||
desc->page_size = page_size;
|
||||
uint64_t quot = (end-base) / page_size;
|
||||
uint32_t order = log_base_2(quot);
|
||||
if(quot & bit_mask_64(order) != 0)
|
||||
{
|
||||
order++;
|
||||
}
|
||||
desc->order = order;
|
||||
|
||||
// allocate linked lists and bit maps
|
||||
desc->free_lists = (linked_list_t*)desc->alloc((order + 1) * sizeof(linked_list_t));
|
||||
if(desc->free_lists == NULL || desc->bit_map == NULL)
|
||||
return BALLOC_STATUS_CANT_ALLOC_MEM;
|
||||
|
||||
return BALLOC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t balloc_alloc(balloc_desc_t* desc, uint32_t page_num, uint64_t* out)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int32_t balloc_free(balloc_desc_t* desc, uint64_t addr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int32_t balloc_mark_used(balloc_desc_t* desc, uint64_t start, uint64_t end)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int32_t balloc_mark_free(balloc_desc_t* desc, uint64_t start, uint64_t end)
|
||||
{
|
||||
|
||||
}
|
|
@ -2,19 +2,58 @@
|
|||
#define _BALLOC_H_
|
||||
|
||||
#include "linked_list.h"
|
||||
#include "k_def.h"
|
||||
#include "bit_ops.h"
|
||||
#include "k_type.h"
|
||||
#include "avl_tree.h"
|
||||
|
||||
/*
|
||||
* A tree allocator for page-size allocation
|
||||
* All
|
||||
*/
|
||||
|
||||
#define BALLOC_STATUS_SUCCESS 0
|
||||
#define BALLOC_STATUS_INVALID_ARGUMENTS 1
|
||||
#define BALLOC_STATUS_NO_AVAILABLE_MEM 2
|
||||
#define BALLOC_STATUS_CANT_ALLOC_MEM 3
|
||||
#define BALLOC_STATUS_INVALID_ALIGNMENT 4
|
||||
|
||||
typedef void *(*balloc_alloc_func)(uint64_t size);
|
||||
|
||||
typedef void (*balloc_free_func)(void *ptr);
|
||||
|
||||
typedef void (*balloc_lock_func)(void);
|
||||
|
||||
typedef void (*balloc_unlock_func)(void);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void* base;
|
||||
uint32_t size;
|
||||
uint32_t granularity;
|
||||
linked_list_t* free_list;
|
||||
uint32_t* bit_map;
|
||||
uint64_t base;
|
||||
uint64_t order;
|
||||
uint64_t page_size;
|
||||
|
||||
linked_list_t *free_lists;
|
||||
uint8_t *bit_map;
|
||||
|
||||
balloc_alloc_func alloc;
|
||||
balloc_free_func free;
|
||||
balloc_lock_func lock;
|
||||
balloc_unlock_func unlock;
|
||||
} balloc_desc_t;
|
||||
|
||||
|
||||
int32_t balloc_init(balloc_desc_t *desc,
|
||||
uint64_t base,
|
||||
uint64_t end,
|
||||
uint64_t page_size,
|
||||
balloc_alloc_func alloc,
|
||||
balloc_free_func free,
|
||||
balloc_lock_func lock,
|
||||
balloc_unlock_func unlock);
|
||||
|
||||
int32_t balloc_alloc(balloc_desc_t* desc, uint32_t page_num, uint64_t* out);
|
||||
|
||||
int32_t balloc_free(balloc_desc_t* desc, uint64_t addr);
|
||||
|
||||
int32_t balloc_mark_used(balloc_desc_t* desc, uint64_t start, uint64_t end);
|
||||
|
||||
int32_t balloc_mark_free(balloc_desc_t* desc, uint64_t start, uint64_t end);
|
||||
|
||||
#endif
|
|
@ -1,7 +1,9 @@
|
|||
#ifndef _S_BOOT_H_
|
||||
#define _S_BOOT_H_
|
||||
|
||||
#include "g_abi.h"
|
||||
#include "s_pmm.h"
|
||||
#include "s_intr.h"
|
||||
|
||||
//
|
||||
// HAL Boot Info
|
||||
|
@ -15,4 +17,6 @@ typedef struct
|
|||
char cpu_vd_str[13];
|
||||
} k_hal_boot_info_t;
|
||||
|
||||
extern void KAPI k_main(k_hal_boot_info_t* info);
|
||||
|
||||
#endif
|
|
@ -29,18 +29,20 @@ typedef uint64_t k_irql_t;
|
|||
#define K_IRQL_APC_LEVEL 1
|
||||
#define K_IRQL_PASSIVE_LEVEL 0
|
||||
|
||||
extern KAPI k_irql_t k_set_irql(k_irql_t irql);
|
||||
extern k_irql_t KAPI k_set_irql(k_irql_t irql);
|
||||
|
||||
extern KAPI k_irql_t k_get_irql();
|
||||
extern k_irql_t KAPI k_get_irql();
|
||||
|
||||
extern KAPI void k_set_timer_timeout(uint64_t timeout);
|
||||
extern void KAPI k_halt_cpu();
|
||||
|
||||
extern void KAPI k_set_timer_timeout(uint64_t timeout);
|
||||
|
||||
// Interrupt handler registration
|
||||
// context is a parameter passed by the kernel. HAL must pass back.
|
||||
// intr_stack is a parameter passed by the HAL. Used by some HAL interrupt context functions.
|
||||
typedef void ( KAPI *k_intr_handler_t)(void *context, void *intr_stack);
|
||||
|
||||
extern void KAPI k_register_intr_handler(uint32_t index, k_intr_handler_t handler);
|
||||
extern void KAPI k_register_intr_handler(uint32_t index, k_intr_handler_t handler, void* context);
|
||||
|
||||
extern k_intr_handler_t KAPI k_deregister_intr_handler(uint32_t index);
|
||||
|
||||
|
@ -51,6 +53,4 @@ extern void KAPI k_register_exc_handler(k_exc_type_t type, k_exc_handler_t handl
|
|||
|
||||
extern k_exc_handler_t KAPI k_deregister_exc_handler(uint64_t index);
|
||||
|
||||
extern void KAPI k_halt_cpu();
|
||||
|
||||
#endif
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "g_abi.h"
|
||||
#include "g_type.h"
|
||||
#include "linked_list.h"
|
||||
|
||||
#define K_PAGE_SIZE 4096
|
||||
|
||||
|
@ -12,12 +13,18 @@ typedef uint64_t k_physical_addr_t;
|
|||
// the size is the # of pages
|
||||
// attr is not useful yet
|
||||
// if unalignment is detected, the kernel bug checks.
|
||||
|
||||
typedef struct
|
||||
{
|
||||
linked_list_node_t list_node;
|
||||
k_physical_addr_t base;
|
||||
uint64_t size;
|
||||
uint32_t attr;
|
||||
} k_pmm_node_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t num_of_nodes;
|
||||
k_pmm_node_t nodes[];
|
||||
} k_pmm_info_t;
|
||||
|
||||
#endif
|
|
@ -6,9 +6,10 @@
|
|||
#ifndef _AVL_TREE_H_
|
||||
#define _AVL_TREE_H_
|
||||
|
||||
#include "g_type.h"
|
||||
#include "g_abi.h"
|
||||
#include "std_lib.h"
|
||||
|
||||
|
||||
typedef struct _avl_tree_node_t
|
||||
{
|
||||
struct _avl_tree_node_t *left;
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
/* Copyright 2016 secXsQuared
|
||||
* Distributed under GPL license
|
||||
* See COPYING under root for details
|
||||
*/
|
||||
|
||||
#include "balloc.h"
|
||||
#include "std_lib.h"
|
||||
#include "bit_ops.h"
|
||||
|
||||
int32_t balloc_init(balloc_desc_t *desc,
|
||||
uint64_t base,
|
||||
uint64_t end,
|
||||
uint64_t page_size,
|
||||
balloc_alloc_func alloc,
|
||||
balloc_free_func free,
|
||||
balloc_lock_func lock,
|
||||
balloc_unlock_func unlock)
|
||||
{
|
||||
if(desc == NULL || base >= end || page_size == 0 || alloc == 0
|
||||
|| free == 0 || lock == 0 || unlock == 0)
|
||||
return BALLOC_STATUS_INVALID_ARGUMENTS;
|
||||
|
||||
if(((end - base) % page_size) != 0)
|
||||
return BALLOC_STATUS_INVALID_ALIGNMENT;
|
||||
|
||||
desc->alloc = alloc;
|
||||
desc->free = free;
|
||||
desc->lock = lock;
|
||||
desc->unlock = unlock;
|
||||
|
||||
desc->base = base;
|
||||
desc->page_size = page_size;
|
||||
uint64_t quot = (end-base) / page_size;
|
||||
uint32_t order = log_base_2(quot);
|
||||
if(quot & bit_mask_64(order) != 0)
|
||||
{
|
||||
order++;
|
||||
}
|
||||
desc->order = order;
|
||||
|
||||
// allocate linked lists and bit maps
|
||||
desc->free_lists = (linked_list_t*)desc->alloc((order + 1) * sizeof(linked_list_t));
|
||||
if(desc->free_lists == NULL || desc->bit_map == NULL)
|
||||
return BALLOC_STATUS_CANT_ALLOC_MEM;
|
||||
|
||||
return BALLOC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t balloc_alloc(balloc_desc_t* desc, uint32_t page_num, uint64_t* out)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int32_t balloc_free(balloc_desc_t* desc, uint64_t addr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int32_t balloc_mark_used(balloc_desc_t* desc, uint64_t start, uint64_t end)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int32_t balloc_mark_free(balloc_desc_t* desc, uint64_t start, uint64_t end)
|
||||
{
|
||||
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
#ifndef _BALLOC_H_
|
||||
#define _BALLOC_H_
|
||||
|
||||
#include "linked_list.h"
|
||||
#include "avl_tree.h"
|
||||
|
||||
/*
|
||||
* A tree allocator for page-size allocation
|
||||
* All
|
||||
*/
|
||||
|
||||
#define BALLOC_STATUS_SUCCESS 0
|
||||
#define BALLOC_STATUS_INVALID_ARGUMENTS 1
|
||||
#define BALLOC_STATUS_NO_AVAILABLE_MEM 2
|
||||
#define BALLOC_STATUS_CANT_ALLOC_MEM 3
|
||||
#define BALLOC_STATUS_INVALID_ALIGNMENT 4
|
||||
|
||||
typedef void *(*balloc_alloc_func)(uint64_t size);
|
||||
|
||||
typedef void (*balloc_free_func)(void *ptr);
|
||||
|
||||
typedef void (*balloc_lock_func)(void);
|
||||
|
||||
typedef void (*balloc_unlock_func)(void);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint64_t base;
|
||||
uint64_t order;
|
||||
uint64_t page_size;
|
||||
|
||||
linked_list_t *free_lists;
|
||||
uint8_t *bit_map;
|
||||
|
||||
balloc_alloc_func alloc;
|
||||
balloc_free_func free;
|
||||
balloc_lock_func lock;
|
||||
balloc_unlock_func unlock;
|
||||
} balloc_desc_t;
|
||||
|
||||
|
||||
int32_t balloc_init(balloc_desc_t *desc,
|
||||
uint64_t base,
|
||||
uint64_t end,
|
||||
uint64_t page_size,
|
||||
balloc_alloc_func alloc,
|
||||
balloc_free_func free,
|
||||
balloc_lock_func lock,
|
||||
balloc_unlock_func unlock);
|
||||
|
||||
int32_t balloc_alloc(balloc_desc_t* desc, uint32_t page_num, uint64_t* out);
|
||||
|
||||
int32_t balloc_free(balloc_desc_t* desc, uint64_t addr);
|
||||
|
||||
int32_t balloc_mark_used(balloc_desc_t* desc, uint64_t start, uint64_t end);
|
||||
|
||||
int32_t balloc_mark_free(balloc_desc_t* desc, uint64_t start, uint64_t end);
|
||||
|
||||
#endif
|
|
@ -7,6 +7,7 @@
|
|||
#define _LINKED_LIST_H_
|
||||
|
||||
#include "g_abi.h"
|
||||
#include "g_type.h"
|
||||
|
||||
typedef struct _linked_list_node_t
|
||||
{
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#define _SALLOC_H_
|
||||
|
||||
#include "g_abi.h"
|
||||
#include "g_type.h"
|
||||
|
||||
void KAPI salloc_init(void *base, uint32_t size);
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
*/
|
||||
|
||||
#include "g_abi.h"
|
||||
#include "g_type.h"
|
||||
#include "bit_ops.h"
|
||||
|
||||
typedef union
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
; Distributed under GPL license
|
||||
; See COPYING under root for details
|
||||
|
||||
extern kmain
|
||||
extern hal_main
|
||||
|
||||
; IMPORTANT: This module should be 4k-page aliened
|
||||
[SECTION .entry]
|
||||
|
@ -232,7 +232,7 @@ mov ss,ax
|
|||
; align 16 bytes like this for now
|
||||
mov rsp,KERNEL_STACK
|
||||
mov rdi,rsi ; multiboot_info*
|
||||
call kmain
|
||||
call hal_main
|
||||
hlt
|
||||
|
||||
align 4096 ;4k alignment
|
||||
|
|
|
@ -7,11 +7,14 @@
|
|||
#include "hal_print.h"
|
||||
#include "hal_mem.h"
|
||||
#include "hal_intr.h"
|
||||
#include "hal_var.h"
|
||||
#include "hal_arch.h"
|
||||
#include "std_lib.h"
|
||||
#include "s_boot.h"
|
||||
|
||||
static void KAPI _hal_obtain_cpu_info(k_boot_info_t *hal_info)
|
||||
extern char kernel_start[];
|
||||
extern char kernel_end[];
|
||||
|
||||
static void KAPI _hal_obtain_cpu_info(k_hal_boot_info_t *hal_info)
|
||||
{
|
||||
if(hal_info == NULL)
|
||||
return;
|
||||
|
@ -23,51 +26,17 @@ static void KAPI _hal_obtain_cpu_info(k_boot_info_t *hal_info)
|
|||
hal_info->cpu_vd_str[12] = 0;
|
||||
}
|
||||
|
||||
static void KAPI _hal_init_gdt()
|
||||
{
|
||||
|
||||
// get gdt ready
|
||||
hal_write_segment_descriptor((void *) &g_gdt[0], 0, 0, 0);
|
||||
hal_write_segment_descriptor((void *) &g_gdt[8], 0, 0,
|
||||
SEG_DPL_0 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_CODE_X);
|
||||
hal_write_segment_descriptor((void *) &g_gdt[16], 0, 0,
|
||||
SEG_DPL_0 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_DATA_RW);
|
||||
hal_write_segment_descriptor((void *) &g_gdt[24], 0, 0,
|
||||
SEG_DPL_3 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_CODE_X);
|
||||
hal_write_segment_descriptor((void *) &g_gdt[32], 0, 0,
|
||||
SEG_DPL_3 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_DATA_RW);
|
||||
|
||||
hal_write_segment_descriptor((void *) &g_gdt[40], 0, 0xFFFFF,
|
||||
SEG_DPL_0 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
|
||||
SEG_TYPE_CODE_X);
|
||||
hal_write_segment_descriptor((void *) &g_gdt[48], 0, 0xFFFFF,
|
||||
SEG_DPL_0 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
|
||||
SEG_TYPE_DATA_RW);
|
||||
hal_write_segment_descriptor((void *) &g_gdt[56], 0, 0xFFFFF,
|
||||
SEG_DPL_3 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
|
||||
SEG_TYPE_CODE_X);
|
||||
hal_write_segment_descriptor((void *) &g_gdt[64], 0, 0xFFFFF,
|
||||
SEG_DPL_3 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
|
||||
SEG_TYPE_DATA_RW);
|
||||
g_gdt_ptr.base = (uint64_t) g_gdt;
|
||||
g_gdt_ptr.limit = GDT_ENTRY_NUM * GDT_ENTRY_SIZE - 1;
|
||||
hal_flush_gdt(&g_gdt_ptr, seg_selector(1, 0), seg_selector(2, 0));
|
||||
};
|
||||
|
||||
void KAPI hal_init(void *m_info)
|
||||
void KAPI hal_main(void *m_info)
|
||||
{
|
||||
if (m_info == NULL || (uint64_t) m_info & bit_field_mask(0, 2))
|
||||
return;
|
||||
|
||||
text_pos = get_pos(0, 0);
|
||||
// init HAL infrastructures
|
||||
hal_print_init();
|
||||
hal_mem_init();
|
||||
|
||||
// set up GDT
|
||||
_hal_init_gdt();
|
||||
|
||||
// set up HAL heap;
|
||||
hal_alloc_init();
|
||||
|
||||
k_boot_info_t* boot_info = halloc(sizeof(k_boot_info_t));
|
||||
k_hal_boot_info_t* boot_info = halloc(sizeof(k_hal_boot_info_t));
|
||||
|
||||
// set up HAL def
|
||||
boot_info->krnl_start = (uint64_t)kernel_start;
|
||||
|
@ -79,8 +48,9 @@ void KAPI hal_init(void *m_info)
|
|||
// init interrupt
|
||||
if(hal_interrupt_init() != 0)
|
||||
{
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
return 0;
|
||||
// pass the control to the kernel
|
||||
k_main(boot_info);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -8,8 +8,14 @@
|
|||
#include "hal_intr.h"
|
||||
#include "hal_print.h"
|
||||
#include "hal_mem.h"
|
||||
#include "hal_var.h"
|
||||
#include "s_intr.h"
|
||||
|
||||
|
||||
static uint8_t _idts[HAL_CORE_COUNT][IDT_ENTRY_NUM*IDT_ENTRY_SIZE];
|
||||
hal_idt_ptr_t _idt_ptrs[HAL_CORE_COUNT];
|
||||
static k_intr_handler_t _intr_handler_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
|
||||
static void* _intr_handler_context_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
|
||||
static k_exc_handler_t _exc_handler_table[HAL_CORE_COUNT][IDT_ENTRY_NUM];
|
||||
|
||||
|
||||
void KAPI hal_write_gate(void *const gate,
|
||||
uint64_t const offset,
|
||||
|
@ -35,22 +41,12 @@ void KAPI hal_write_gate(void *const gate,
|
|||
return;
|
||||
}
|
||||
|
||||
void KAPI hal_set_interrupt_priority(uint64_t priority)
|
||||
{
|
||||
hal_write_cr8(priority & bit_field_mask(0,3));
|
||||
}
|
||||
|
||||
uint64_t KAPI hal_read_interrupt_priority()
|
||||
{
|
||||
return (uint64_t)hal_read_cr8() & bit_field_mask(0,3);
|
||||
}
|
||||
|
||||
void KAPI hal_set_interrupt_handler(uint64_t index,
|
||||
void (*handler)(void))
|
||||
{
|
||||
if (index < IDT_ENTRY_NUM)
|
||||
{
|
||||
hal_write_gate(g_idt + 16 * index, (uint64_t) handler, seg_selector(1, 0),
|
||||
hal_write_gate(_idts[hal_get_core_id()] + 16 * index, (uint64_t) handler, seg_selector(1, 0),
|
||||
GATE_DPL_0 | GATE_PRESENT | GATE_TYPE_INTERRUPT);
|
||||
}
|
||||
return;
|
||||
|
@ -61,39 +57,39 @@ void KAPI hal_issue_interrupt(uint32_t target_core, uint32_t vector)
|
|||
// TODO
|
||||
}
|
||||
|
||||
void KAPI hal_register_interrupt_handler(uint32_t index, hal_intr_handler_t handler, void* context)
|
||||
void KAPI hal_register_interrupt_handler(uint32_t coreid, uint32_t index, k_intr_handler_t handler, void* context)
|
||||
{
|
||||
if (index < IDT_ENTRY_NUM && index >= 0)
|
||||
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
|
||||
{
|
||||
g_intr_handler_table[index] = handler;
|
||||
g_intr_handler_context_table[index] = handler;
|
||||
_intr_handler_table[coreid][index] = handler;
|
||||
_intr_handler_context_table[coreid][index] = context;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void KAPI hal_deregister_interrupt_handler(uint32_t index)
|
||||
void KAPI hal_deregister_interrupt_handler(uint32_t coreid, uint32_t index)
|
||||
{
|
||||
if (index < IDT_ENTRY_NUM && index >= 0)
|
||||
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
|
||||
{
|
||||
g_intr_handler_table[index] = NULL;
|
||||
_intr_handler_table[coreid][index] = NULL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void KAPI hal_register_exception_handler(uint32_t index, k_exc_handler_t handler)
|
||||
void KAPI hal_register_exception_handler(uint32_t coreid, uint32_t index, k_exc_handler_t handler)
|
||||
{
|
||||
if (index < IDT_ENTRY_NUM && index >= 0)
|
||||
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
|
||||
{
|
||||
g_exc_handler_table[index] = handler;
|
||||
_exc_handler_table[coreid][index] = handler;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void KAPI hal_deregister_exception_handler(uint32_t index)
|
||||
void KAPI hal_deregister_exception_handler(uint32_t coreid, uint32_t index)
|
||||
{
|
||||
if (index < IDT_ENTRY_NUM && index >= 0)
|
||||
if (index < IDT_ENTRY_NUM && coreid < HAL_CORE_COUNT)
|
||||
{
|
||||
g_exc_handler_table[index] = NULL;
|
||||
_exc_handler_table[coreid][index] = NULL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -109,15 +105,30 @@ void KAPI hal_assert(int64_t expression,
|
|||
return;
|
||||
}
|
||||
|
||||
void KAPI hal_interrupt_dispatcher(uint64_t int_vec, hal_intr_context_t *context, uint64_t error_code)
|
||||
void KAPI hal_interrupt_dispatcher(uint64_t int_vec, hal_intr_context_t *context)
|
||||
{
|
||||
if (g_intr_handler_table[int_vec] == NULL)
|
||||
uint32_t coreid = hal_get_core_id();
|
||||
if (_intr_handler_table[int_vec] == NULL)
|
||||
{
|
||||
hal_printf("Unhandled interrupt %d at 0x%X. Err: %d.\n", int_vec, context->rip, error_code);
|
||||
hal_printf("Unhandled interrupt %d at 0x%X.\n", int_vec, context->rip);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_intr_handler_table[int_vec](context->rip, context->rsp, int_vec);
|
||||
_intr_handler_table[coreid][int_vec](context, _intr_handler_context_table[coreid][int_vec]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void KAPI hal_exception_dispatcher(uint64_t exc_vec, hal_intr_context_t* context, uint64_t errorcode)
|
||||
{
|
||||
uint32_t coreid = hal_get_core_id();
|
||||
if (_exc_handler_table[exc_vec] == NULL)
|
||||
{
|
||||
hal_printf("Unhandled exception %d at 0x%X.\n", exc_vec, context->rip);
|
||||
}
|
||||
else
|
||||
{
|
||||
_exc_handler_table[coreid][exc_vec](context->rip, context->rsp, errorcode);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -383,31 +394,39 @@ static void KAPI _hal_populate_idt()
|
|||
return;
|
||||
}
|
||||
|
||||
uint32_t KAPI hal_get_core_id(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t KAPI hal_interrupt_init(void)
|
||||
{
|
||||
uint32_t coreid = hal_get_core_id();
|
||||
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
|
||||
eax = 1;
|
||||
hal_cpuid(&eax, &ebx, &ecx, &edx);
|
||||
if (!(edx & bit_mask_32(9)))
|
||||
if (!(edx & bit_mask(9)))
|
||||
{
|
||||
hal_printf("ERROR: APIC not supported by CPU.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// get idt ptr ready
|
||||
g_idt_ptr.base = (uint64_t) g_idt;
|
||||
g_idt_ptr.limit = IDT_ENTRY_NUM * IDT_ENTRY_SIZE - 1;
|
||||
_idt_ptrs[coreid].base = (uint64_t) &_idts[coreid];
|
||||
_idt_ptrs[coreid].limit = IDT_ENTRY_NUM * IDT_ENTRY_SIZE - 1;
|
||||
|
||||
// clear dispatch table
|
||||
for (uint64_t i = 0; i < IDT_ENTRY_NUM; i++)
|
||||
{
|
||||
g_intr_handler_table[i] = NULL;
|
||||
_intr_handler_table[coreid][i] = NULL;
|
||||
_exc_handler_table[coreid][i] = NULL;
|
||||
_intr_handler_context_table[coreid][i] = NULL;
|
||||
}
|
||||
|
||||
// hook asm interrupt handlers
|
||||
_hal_populate_idt();
|
||||
|
||||
hal_flush_idt(&g_idt_ptr);
|
||||
hal_flush_idt(&_idt_ptrs[coreid]);
|
||||
|
||||
// disable PIC
|
||||
hal_write_port_8(0xa1, 0xff);
|
||||
|
@ -418,19 +437,19 @@ int32_t KAPI hal_interrupt_init(void)
|
|||
ecx = MSR_IA32_APIC_BASE;
|
||||
hal_read_msr(&ecx, &edx, &eax);
|
||||
apic_base_reg = ((uint64_t) edx << 32) + (uint64_t) eax;
|
||||
apic_base = apic_base_reg & bit_field_mask_64(12, 35);
|
||||
apic_base = apic_base_reg & bit_field_mask(12, 35);
|
||||
//hal_printf("APIC Base: 0x%X\n", apic_base);
|
||||
//hal_printf("APIC Enabled: %s\n", apic_base_reg & bit_mask_64(11) ? "Yes" : "No");
|
||||
//hal_printf("BSP: %s\n", apic_base_reg & bit_mask_64(8) ? "Yes" : "No");
|
||||
//hal_printf("APIC Spour: 0x%X\n", *(uint32_t *) ((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET));
|
||||
// hardware enable APIC
|
||||
ecx = MSR_IA32_APIC_BASE;
|
||||
eax = (uint32_t) (apic_base_reg & bit_field_mask_64(0, 31)) | bit_mask_32(11);
|
||||
eax = (uint32_t) ((apic_base_reg & bit_field_mask(0, 31)) | bit_mask(11));
|
||||
hal_write_msr(&ecx, &edx, &eax);
|
||||
|
||||
// software enable APIC
|
||||
hal_write_mem_32((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET,
|
||||
*(uint32_t *) ((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET) | bit_mask_32(8));
|
||||
*(uint32_t *) ((char *) apic_base + APIC_SPURIOUS_INT_VEC_REG_OFFSET) | bit_mask(8));
|
||||
|
||||
// hal_issue_interrupt(1, 255);
|
||||
// hal_enable_interrupt();
|
||||
|
|
|
@ -7,4 +7,19 @@
|
|||
uint64_t KAPI k_interlocked_exchange(uint64_t* target, uint64_t val)
|
||||
{
|
||||
return hal_interlocked_exchange(target, val);
|
||||
}
|
||||
|
||||
k_irql_t KAPI k_set_irql(k_irql_t irql)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
k_irql_t KAPI k_get_irql()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void KAPI k_halt_cpu()
|
||||
{
|
||||
hal_halt_cpu();
|
||||
}
|
|
@ -7,6 +7,11 @@
|
|||
#include "g_type.h"
|
||||
#include "hal_mem.h"
|
||||
#include "salloc.h"
|
||||
#include "hal_arch.h"
|
||||
#include "hal_intr.h"
|
||||
|
||||
static uint8_t _gdts[HAL_CORE_COUNT][GDT_ENTRY_NUM*GDT_ENTRY_SIZE];
|
||||
static hal_gdt_ptr_t _gdt_ptrs[HAL_CORE_COUNT];
|
||||
|
||||
#define KERNEL_HEAP_SIZE 8192
|
||||
|
||||
|
@ -106,8 +111,40 @@ void KAPI hfree(void *ptr)
|
|||
return;
|
||||
}
|
||||
|
||||
void KAPI hal_alloc_init()
|
||||
static void KAPI _hal_init_gdt()
|
||||
{
|
||||
uint32_t coreid = hal_get_core_id();
|
||||
// get gdt ready
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][0], 0, 0, 0);
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][8], 0, 0,
|
||||
SEG_DPL_0 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_CODE_X);
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][16], 0, 0,
|
||||
SEG_DPL_0 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_DATA_RW);
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][24], 0, 0,
|
||||
SEG_DPL_3 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_CODE_X);
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][32], 0, 0,
|
||||
SEG_DPL_3 | SEG_CODE_DATA | SEG_PRESENT | SEG_LONG | SEG_TYPE_DATA_RW);
|
||||
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][40], 0, 0xFFFFF,
|
||||
SEG_DPL_0 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
|
||||
SEG_TYPE_CODE_X);
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][48], 0, 0xFFFFF,
|
||||
SEG_DPL_0 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
|
||||
SEG_TYPE_DATA_RW);
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][56], 0, 0xFFFFF,
|
||||
SEG_DPL_3 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
|
||||
SEG_TYPE_CODE_X);
|
||||
hal_write_segment_descriptor((void *) &_gdts[coreid][64], 0, 0xFFFFF,
|
||||
SEG_DPL_3 | SEG_GRANULARITY | SEG_CODE_DATA | SEG_PRESENT | SEG_32_BITS |
|
||||
SEG_TYPE_DATA_RW);
|
||||
_gdt_ptrs[coreid].base = (uint64_t) &_gdts[coreid];
|
||||
_gdt_ptrs[coreid].limit = GDT_ENTRY_NUM * GDT_ENTRY_SIZE - 1;
|
||||
hal_flush_gdt(&_gdt_ptrs[coreid], seg_selector(1, 0), seg_selector(2, 0));
|
||||
};
|
||||
|
||||
void KAPI hal_mem_init()
|
||||
{
|
||||
_hal_init_gdt();
|
||||
salloc_init(kernel_heap, KERNEL_HEAP_SIZE);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -6,7 +6,13 @@
|
|||
#include "g_abi.h"
|
||||
#include "std_lib.h"
|
||||
#include "hal_print.h"
|
||||
#include "hal_var.h"
|
||||
|
||||
static uint64_t text_pos;
|
||||
|
||||
void KAPI hal_print_init()
|
||||
{
|
||||
text_pos = 0;
|
||||
}
|
||||
|
||||
void KAPI _hal_print_scroll()
|
||||
{
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
/* Copyright 2016 secXsQuared
|
||||
* Distributed under GPL license
|
||||
* See COPYING under root for details
|
||||
*/
|
||||
|
||||
#include "hal_intr.h"
|
||||
#include "hal_var.h"
|
||||
|
||||
uint8_t g_gdt[GDT_ENTRY_NUM*GDT_ENTRY_SIZE];
|
||||
uint8_t g_idt[IDT_ENTRY_NUM*IDT_ENTRY_SIZE];
|
||||
k_intr_handler_t g_intr_handler_table[IDT_ENTRY_NUM];
|
||||
k_exc_handler_t g_exc_handler_table[IDT_ENTRY_NUM];
|
||||
hal_gdt_ptr_t g_gdt_ptr;
|
||||
hal_idt_ptr_t g_idt_ptr;
|
||||
uint64_t text_pos;
|
|
@ -2,6 +2,9 @@
|
|||
#define _HAL_ARCH_H_
|
||||
|
||||
#include "g_abi.h"
|
||||
#include "g_type.h"
|
||||
|
||||
#define HAL_CORE_COUNT 1
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#ifndef _HAL_IO_H_
|
||||
#define _HAL_IO_H_
|
||||
|
||||
#include <s_intr.h>
|
||||
#include "s_intr.h"
|
||||
#include "g_abi.h"
|
||||
#include "g_type.h"
|
||||
|
||||
|
@ -31,8 +31,6 @@
|
|||
#define APIC_LVT_LINT1_REG 0x360
|
||||
#define APIC_LVT_ERROR_REG 0x370
|
||||
|
||||
typedef k_intr_handler_t hal_intr_handler_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const uint64_t rip;
|
||||
|
@ -556,11 +554,15 @@ extern void KAPI hal_interrupt_handler_254(void);
|
|||
|
||||
extern void KAPI hal_interrupt_handler_255(void);
|
||||
|
||||
void KAPI hal_register_interrupt_handler(uint32_t index, hal_intr_handler_t handler, void* context);
|
||||
void KAPI hal_register_interrupt_handler(uint32_t coreid, uint32_t index, k_intr_handler_t handler, void* context);
|
||||
|
||||
void KAPI hal_deregister_interrupt_handler(uint32_t index);
|
||||
void KAPI hal_deregister_interrupt_handler(uint32_t coreid, uint32_t index);
|
||||
|
||||
void KAPI hal_issue_interrupt(uint32_t target_core, uint32_t vector);
|
||||
void KAPI hal_register_exception_handler(uint32_t coreid, uint32_t index, k_exc_handler_t handler);
|
||||
|
||||
void KAPI hal_deregister_exception_handler(uint32_t coreid, uint32_t index);
|
||||
|
||||
void KAPI hal_issue_interrupt(uint32_t core_id, uint32_t vector);
|
||||
|
||||
void KAPI hal_set_interrupt_handler(uint64_t index, void (*handler)(void));
|
||||
|
||||
|
@ -570,4 +572,6 @@ void KAPI hal_assert(int64_t exp, char *message);
|
|||
|
||||
int32_t KAPI hal_interrupt_init(void);
|
||||
|
||||
uint32_t KAPI hal_get_core_id(void);
|
||||
|
||||
#endif
|
|
@ -85,7 +85,7 @@ void* KAPI halloc(uint32_t size);
|
|||
|
||||
void KAPI hfree(void *ptr);
|
||||
|
||||
void KAPI hal_alloc_init();
|
||||
void KAPI hal_mem_init();
|
||||
|
||||
void KAPI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit, uint64_t const attr);
|
||||
|
||||
|
|
|
@ -12,7 +12,8 @@
|
|||
#define get_row(pos) (pos / 80)
|
||||
#define get_pos(row,col) ((row) * 80 + (col))
|
||||
|
||||
void KAPI hal_clear_screen(void);
|
||||
void KAPI hal_clear_screen();
|
||||
void KAPI hal_print_init();
|
||||
void KAPI hal_printf(char const *format, ...);
|
||||
|
||||
#endif
|
|
@ -1,25 +0,0 @@
|
|||
/* Copyright 2016 secXsQuared
|
||||
* Distributed under GPL license
|
||||
* See COPYING under root for details
|
||||
*/
|
||||
|
||||
#ifndef _HAL_VAR_H_
|
||||
#define _HAL_VAR_H_
|
||||
|
||||
#include "hal_mem.h"
|
||||
#include "linked_list.h"
|
||||
#include "hal_arch.h"
|
||||
#include "hal_intr.h"
|
||||
|
||||
extern uint8_t g_gdt[GDT_ENTRY_NUM*GDT_ENTRY_SIZE];
|
||||
extern uint8_t g_idt[IDT_ENTRY_NUM*IDT_ENTRY_SIZE];
|
||||
extern k_intr_handler_t g_intr_handler_table[IDT_ENTRY_NUM];
|
||||
extern void* g_intr_handler_context_table[IDT_ENTRY_NUM];
|
||||
extern k_exc_handler_t g_exc_handler_table[IDT_ENTRY_NUM];
|
||||
extern hal_gdt_ptr_t g_gdt_ptr;
|
||||
extern hal_idt_ptr_t g_idt_ptr;
|
||||
extern uint64_t text_pos;
|
||||
extern char kernel_start[];
|
||||
extern char kernel_end[];
|
||||
|
||||
#endif
|
|
@ -1,4 +1,5 @@
|
|||
#include "g_abi.h"
|
||||
#include "g_type.h"
|
||||
#include "k_alloc.h"
|
||||
#include "salloc.h"
|
||||
|
||||
|
|
|
@ -3,28 +3,25 @@
|
|||
* See COPYING under root for details
|
||||
*/
|
||||
|
||||
#include "s_boot.h"
|
||||
#include "k_alloc.h"
|
||||
#include "k_intr.h"
|
||||
#include "k_lib_test.h"
|
||||
|
||||
extern void KAPI hal_printf(char const *, ...);
|
||||
|
||||
// returning from this function results in halting the cpu
|
||||
void KAPI kmain(void *multiboot_info)
|
||||
void KAPI k_main(k_hal_boot_info_t *boot_info)
|
||||
{
|
||||
// init kernel heap
|
||||
k_alloc_init();
|
||||
|
||||
k_boot_info_t* boot_info = (k_boot_info_t*)k_alloc(sizeof(boot_info));
|
||||
|
||||
if(boot_info == NULL)
|
||||
{
|
||||
hal_printf("KERNEL: Unable to allocated memory for boot info struct.\n");
|
||||
hal_halt_cpu();
|
||||
// failed.
|
||||
hal_printf("KERNEL: HAL init failed.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if(hal_init(multiboot_info, boot_info) != 0)
|
||||
{
|
||||
hal_printf("KERNEL: HAL initialization failed.\n");
|
||||
hal_halt_cpu();
|
||||
}
|
||||
// init kernel heap
|
||||
k_alloc_init();
|
||||
|
||||
hal_printf("KERNEL: Base Addr is 0x%X. Size is %uB, %uKB.\n",
|
||||
boot_info->krnl_start,
|
||||
|
@ -38,5 +35,6 @@ void KAPI kmain(void *multiboot_info)
|
|||
salloc_test();
|
||||
|
||||
hal_printf("KERNEL: Kernel tasks finished.\n");
|
||||
hal_halt_cpu();
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ int32_t KAPI k_pmm_init(k_pmm_info_t *info, k_pmm_descriptor_t *desc)
|
|||
|
||||
linked_list_init(&desc->free_list);
|
||||
avl_tree_init(&desc->active_tree, _avl_compare);
|
||||
for (int i = 0; i < info->num_of_nodes; i++)
|
||||
for (uint32_t i = 0; i < info->num_of_nodes; i++)
|
||||
{
|
||||
k_pmm_node_t *each_node = &info->nodes[i];
|
||||
|
||||
|
@ -52,7 +52,7 @@ int32_t KAPI k_pmm_init(k_pmm_info_t *info, k_pmm_descriptor_t *desc)
|
|||
return PMM_STATUS_INIT_UNALIGNED;
|
||||
}
|
||||
|
||||
for (int j = 0; j <= each_node->size; j++)
|
||||
for (uint64_t j = 0; j <= each_node->size; j++)
|
||||
{
|
||||
// note that k_alloc function here might trigger page fault
|
||||
// however it's fine as long as we don't touch linked list just yet
|
||||
|
|
|
@ -72,10 +72,10 @@ int32_t k_alloc_virtual_address(k_vmm_descriptor_t *desc,
|
|||
|
||||
int64_t k_query_virtual_address(k_vmm_descriptor_t *desc, k_virtual_addr_t v_addr, uint64_t *out)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t k_free_virtual_address(k_vmm_descriptor_t *desc, k_virtual_addr_t base)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -400,7 +400,7 @@ static bool push_pop_back_test()
|
|||
|
||||
static bool equals(linked_list_node_t *a, linked_list_node_t *b)
|
||||
{
|
||||
return (int64_t) a == OBTAIN_STRUCT_ADDR(b, my_list_node, lnode)->val;
|
||||
return (int64_t) b == OBTAIN_STRUCT_ADDR(a, my_list_node, lnode)->val;
|
||||
}
|
||||
|
||||
static bool search_test()
|
||||
|
|
Loading…
Reference in New Issue