[IN PROGRESS] Added hal_interlocked_exchange and implemented spin locks in C code. Reverted some kernel_status_t changes.

This commit is contained in:
hyperassembler 2015-11-14 17:53:30 -05:00
parent cce6d1558f
commit 93cd12690c
8 changed files with 176 additions and 145 deletions

View File

@ -1,17 +0,0 @@
global hal_spin_lock;
global hal_spin_unlock;
;void hal_spin_lock(uint32_t * lock)
hal_spin_lock:
xor rcx,rcx
inc rcx
.spin:
xor rax,rax
lock cmpxchg dword [rdi],ecx
jnz .spin
ret
;void hal_spin_unlock(uint32_t * lock)
hal_spin_unlock:
mov dword [rdi],0
ret

7
x64/src/asm/hal/sync.asm Normal file
View File

@ -0,0 +1,7 @@
global hal_interlocked_exchange;
;uint64_t _KERNEL_ABI hal_interlocked_exchange(_IN _OUT uint64_t* dst, _IN uint64_t val);
hal_interlocked_exchange:
lock xchg qword [rdi], rsi
mov rax, rsi
ret

View File

@ -7,46 +7,58 @@
#include "../common/lib/mem.h"
#include "../common/sys/sys_info.h"
boot_info_t*_KERNEL_ABI hal_init(multiboot_info_t* m_info)
boot_info_t *_KERNEL_ABI hal_init(multiboot_info_t *m_info)
{
if (m_info == NULL)
return NULL;
boot_info_t *boot_info = (boot_info_t *) hal_halloc(sizeof(boot_info_t));
text_pos = get_pos(0, 0);
// 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[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 = 8*9-1;
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 = 8 * 9 - 1;
hal_flush_gdt(&g_gdt_ptr, SEG_SELECTOR(1, 0), SEG_SELECTOR(2, 0));
// get idt ptr ready
g_idt_ptr.base = (uint64_t)g_idt;
g_idt_ptr.limit = 21*16-1;
g_idt_ptr.base = (uint64_t) g_idt;
g_idt_ptr.limit = 21 * 16 - 1;
hal_flush_idt(&g_idt_ptr);
boot_info_t* boot_info = (boot_info_t*)hal_halloc(sizeof(boot_info_t));
hal_assert(boot_info != NULL, "Unable to allocate memory for boot_info.");
mem_set(boot_info,0, sizeof(boot_info_t));
mem_set(boot_info, 0, sizeof(boot_info_t));
// obtain boot information
// memory info
if(m_info->flags & (1 << 6))
if (m_info->flags & (1 << 6))
{
boot_info->mem_info = (mem_info_t*)hal_halloc(sizeof(mem_info_t));
boot_info->mem_info = (mem_info_t *) hal_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*)hal_halloc((sizeof(linked_list_t)));
boot_info->mem_info->occupied_page_list = (linked_list_t*)hal_halloc((sizeof(linked_list_t)));
boot_info->mem_info->free_page_list = (linked_list_t *) hal_halloc((sizeof(linked_list_t)));
boot_info->mem_info->occupied_page_list = (linked_list_t *) hal_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.");
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 const *mem_map = (multiboot_memory_map_t *) m_info->mmap_addr;
@ -54,7 +66,7 @@ boot_info_t*_KERNEL_ABI hal_init(multiboot_info_t* m_info)
for (int i = 0; i < mem_map_size; i++)
{
hal_printf("\n==Base: 0x%X, Length: %u, Type: %s==", (mem_map + i)->addr, (mem_map + i)->len,
(mem_map + i)->type== MULTIBOOT_MEMORY_AVAILABLE ? "AVL" : "RSV");
(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;
@ -66,9 +78,9 @@ boot_info_t*_KERNEL_ABI hal_init(multiboot_info_t* m_info)
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;
uint64_t page_count = (aligned_end_addr - aligned_base_addr) / PHYSICAL_PAGE_SIZE;
if(page_count == 0)
if (page_count == 0)
continue;
// strip kernel-occupied pages
@ -98,12 +110,13 @@ boot_info_t*_KERNEL_ABI hal_init(multiboot_info_t* m_info)
// }
// }
memory_descriptor_node_t* each_desc = (memory_descriptor_node_t*)hal_halloc(sizeof(memory_descriptor_node_t));
memory_descriptor_node_t *each_desc = (memory_descriptor_node_t *) hal_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_add(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_available += aligned_end_addr - aligned_base_addr;
}
boot_info->mem_info->mem_installed += (mem_map + i)->len;
}
@ -116,25 +129,27 @@ boot_info_t*_KERNEL_ABI hal_init(multiboot_info_t* m_info)
}
// loaded kernel modules
if(m_info->flags & (1 << 3))
if (m_info->flags & (1 << 3))
{
boot_info->module_info = (module_info_t*)hal_halloc(sizeof(module_info_t));
boot_info->module_info = (module_info_t *) hal_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*)hal_halloc(sizeof(linked_list_t));
boot_info->module_info->module_list = (linked_list_t *) hal_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 const * mods_list = (multiboot_module_t *)m_info->mods_addr;
multiboot_module_t const *mods_list = (multiboot_module_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*)hal_halloc(sizeof(module_descriptor_node_t));
module_descriptor_node_t *each_module = (module_descriptor_node_t *) hal_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*)hal_halloc((size_t)str_len((char *) (mods_list + i)->cmdline) + 1);
each_module->name = (char *) hal_halloc((size_t) str_len((char *) (mods_list + i)->cmdline) + 1);
hal_assert(each_module->name != NULL, "Unable to allocate memory for module name string.");
mem_copy((void*)(mods_list + i)->cmdline, each_module->name, str_len((char *) (mods_list + i)->cmdline) + 1);
mem_copy((void *) (mods_list + i)->cmdline, each_module->name,
str_len((char *) (mods_list + i)->cmdline) + 1);
linked_list_add(boot_info->module_info->module_list, &each_module->list_node);
}
}
@ -145,8 +160,8 @@ boot_info_t*_KERNEL_ABI hal_init(multiboot_info_t* m_info)
cpuid_info.ebx = 0;
cpuid_info.ecx = 0;
cpuid_info.edx = 0;
hal_cpuid(&cpuid_info.eax,&cpuid_info.ebx,&cpuid_info.ecx,&cpuid_info.edx);
if(cpuid_info.edx & 1 << 9)
hal_cpuid(&cpuid_info.eax, &cpuid_info.ebx, &cpuid_info.ecx, &cpuid_info.edx);
if (cpuid_info.edx & 1 << 9)
{
//TODO: detected.
}
@ -158,4 +173,23 @@ boot_info_t*_KERNEL_ABI hal_init(multiboot_info_t* m_info)
}
return boot_info;
}
}
void _KERNEL_ABI hal_spin_lock(uint64_t *lock)
{
if (lock != NULL)
{
while (hal_interlocked_exchange(lock, 1) == 1)
{ };
}
return;
}
void _KERNEL_ABI hal_spin_unlock(uint64_t *lock)
{
if (lock != NULL)
{
*lock = 0;
}
return;
}

View File

@ -5,10 +5,14 @@
#include "multiboot.h"
#include "../common/sys/sys_info.h"
//concurrency
extern void _KERNEL_ABI hal_spin_lock(uint32_t * lock);
extern void _KERNEL_ABI hal_spin_unlock(uint32_t * lock);
// concurrency
void _KERNEL_ABI hal_spin_lock(uint64_t * lock);
void _KERNEL_ABI hal_spin_unlock(uint64_t * lock);
// Atomically set *dst = val
// return: the previous value of *dst
extern uint64_t _KERNEL_ABI hal_interlocked_exchange(_IN _OUT uint64_t* dst,
_IN uint64_t val);
// loaded kernel addr
extern char kernel_start[];
extern char kernel_end[];

View File

@ -8,44 +8,45 @@ void hal_interrupt_handler_dummy(void)
}
kernel_status_t _KERNEL_ABI hal_write_gate(_IN void *const gate,
_IN uint64_t const offset,
_IN uint32_t const selector,
_IN uint32_t const attr)
void _KERNEL_ABI hal_write_gate(_IN void *const gate,
_IN uint64_t const offset,
_IN uint32_t const selector,
_IN uint32_t const attr)
{
((uint8_t*)gate)[0] = (uint8_t)(offset & 0xFF);
((uint8_t*)gate)[1] = (uint8_t)((offset >> 8) & 0xFF);
((uint8_t*)gate)[2] = (uint8_t)(selector & 0xFF);
((uint8_t*)gate)[3] = (uint8_t)((selector >> 8) & 0xFF);
((uint8_t*)gate)[4] = (uint8_t)(attr & 0xFF);
((uint8_t*)gate)[5] = (uint8_t)((attr >> 8) & 0xFF);
((uint8_t*)gate)[6] = (uint8_t)((offset >> 16) & 0xFF);
((uint8_t*)gate)[7] = (uint8_t)((offset >> 24) & 0xFF);
((uint8_t*)gate)[8] = (uint8_t)((offset >> 32) & 0xFF);
((uint8_t*)gate)[9] = (uint8_t)((offset >> 40) & 0xFF);
((uint8_t*)gate)[10] = (uint8_t)((offset >> 48) & 0xFF);
((uint8_t*)gate)[11] = (uint8_t)((offset >> 56) & 0xFF);
((uint8_t*)gate)[12] = 0;
((uint8_t*)gate)[13] = 0;
((uint8_t*)gate)[14] = 0;
((uint8_t*)gate)[15] = 0;
return KERNEL_STATUS_SUCCESS;
((uint8_t *) gate)[0] = (uint8_t) (offset & 0xFF);
((uint8_t *) gate)[1] = (uint8_t) ((offset >> 8) & 0xFF);
((uint8_t *) gate)[2] = (uint8_t) (selector & 0xFF);
((uint8_t *) gate)[3] = (uint8_t) ((selector >> 8) & 0xFF);
((uint8_t *) gate)[4] = (uint8_t) (attr & 0xFF);
((uint8_t *) gate)[5] = (uint8_t) ((attr >> 8) & 0xFF);
((uint8_t *) gate)[6] = (uint8_t) ((offset >> 16) & 0xFF);
((uint8_t *) gate)[7] = (uint8_t) ((offset >> 24) & 0xFF);
((uint8_t *) gate)[8] = (uint8_t) ((offset >> 32) & 0xFF);
((uint8_t *) gate)[9] = (uint8_t) ((offset >> 40) & 0xFF);
((uint8_t *) gate)[10] = (uint8_t) ((offset >> 48) & 0xFF);
((uint8_t *) gate)[11] = (uint8_t) ((offset >> 56) & 0xFF);
((uint8_t *) gate)[12] = 0;
((uint8_t *) gate)[13] = 0;
((uint8_t *) gate)[14] = 0;
((uint8_t *) gate)[15] = 0;
return;
}
kernel_status_t _KERNEL_ABI hal_set_interrupt_handler(_IN uint64_t index,
_IN void (*handler)(void))
void _KERNEL_ABI hal_set_interrupt_handler(_IN uint64_t index,
_IN void (*handler)(void))
{
hal_write_gate(g_idt + 16*index, (uint64_t)handler, SEG_SELECTOR(1,0), GATE_DPL_0 | GATE_PRESENT | GATE_TYPE_INTERRUPT);
return KERNEL_STATUS_SUCCESS;
hal_write_gate(g_idt + 16 * index, (uint64_t) handler, SEG_SELECTOR(1, 0),
GATE_DPL_0 | GATE_PRESENT | GATE_TYPE_INTERRUPT);
return;
}
kernel_status_t _KERNEL_ABI hal_assert(_IN int64_t expression,
_IN_OPT char* message)
void _KERNEL_ABI hal_assert(_IN int64_t expression,
_IN_OPT char *message)
{
if(!expression)
if (!expression)
{
hal_printf("HAL: Assertion failed. Detail: %s", message == NULL ? "NULL" : message);
hal_halt_cpu();
}
return KERNEL_STATUS_SUCCESS;
return;
}

View File

@ -16,16 +16,16 @@ extern void _KERNEL_ABI hal_write_port(uint64_t port, int64_t data);
extern int64_t _KERNEL_ABI hal_read_port(uint64_t port);
void _KERNEL_ABI hal_interrupt_handler_dummy();
kernel_status_t _KERNEL_ABI hal_set_interrupt_handler(uint64_t index, void (*handler)());
void _KERNEL_ABI hal_set_interrupt_handler(uint64_t index, void (*handler)());
extern void _KERNEL_ABI hal_enable_interrupt();
extern void _KERNEL_ABI hal_disable_interrupt();
extern void _KERNEL_ABI hal_interrupt_handler_wrapper();
extern void _KERNEL_ABI hal_halt_cpu();
kernel_status_t _KERNEL_ABI hal_write_gate(void *const gate, uint64_t const offset, uint32_t const selector, uint32_t const attr);
void _KERNEL_ABI hal_write_gate(void *const gate, uint64_t const offset, uint32_t const selector, uint32_t const attr);
//assert
kernel_status_t _KERNEL_ABI hal_assert(int64_t exp, char* message);
void _KERNEL_ABI hal_assert(int64_t exp, char* message);
extern uint8_t g_idt[];
#endif

View File

@ -3,90 +3,93 @@
#include "mem.h"
#define kernel_heap_size 4096
char* _cur_heap = NULL;
char *_cur_heap = NULL;
extern char kernel_heap[kernel_heap_size];
void _KERNEL_ABI hal_write_pt_entry(void *const base, uint64_t const p_addr, uint64_t const attr)
{
if(base == NULL)
if (base == NULL)
return;
uint64_t entry = (p_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t*)base)[0] = (uint8_t)(entry & 0xFF);
((uint8_t*)base)[1] = (uint8_t)((entry >> 8) & 0xFF);
((uint8_t*)base)[2] = (uint8_t)((entry >> 16) & 0xFF);
((uint8_t*)base)[3] = (uint8_t)((entry >> 24) & 0xFF);
((uint8_t*)base)[4] = (uint8_t)((entry >> 32) & 0xFF);
((uint8_t*)base)[5] = (uint8_t)((entry >> 40) & 0xFF);
((uint8_t*)base)[6] = (uint8_t)((entry >> 48) & 0xFF);
((uint8_t*)base)[7] = (uint8_t)((entry >> 56) & 0xFF);
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
}
void _KERNEL_ABI hal_write_pd_entry(void *const base, uint64_t const pt_addr, uint64_t const attr)
{
if(base == NULL)
if (base == NULL)
return;
uint64_t entry = (pt_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t*)base)[0] = (uint8_t)(entry & 0xFF);
((uint8_t*)base)[1] = (uint8_t)((entry >> 8) & 0xFF);
((uint8_t*)base)[2] = (uint8_t)((entry >> 16) & 0xFF);
((uint8_t*)base)[3] = (uint8_t)((entry >> 24) & 0xFF);
((uint8_t*)base)[4] = (uint8_t)((entry >> 32) & 0xFF);
((uint8_t*)base)[5] = (uint8_t)((entry >> 40) & 0xFF);
((uint8_t*)base)[6] = (uint8_t)((entry >> 48) & 0xFF);
((uint8_t*)base)[7] = (uint8_t)((entry >> 56) & 0xFF);
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
}
void _KERNEL_ABI hal_write_pdpt_entry(void *const base, uint64_t const pd_addr, uint64_t const attr)
{
if(base == NULL)
if (base == NULL)
return;
uint64_t entry = (pd_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t*)base)[0] = (uint8_t)(entry & 0xFF);
((uint8_t*)base)[1] = (uint8_t)((entry >> 8) & 0xFF);
((uint8_t*)base)[2] = (uint8_t)((entry >> 16) & 0xFF);
((uint8_t*)base)[3] = (uint8_t)((entry >> 24) & 0xFF);
((uint8_t*)base)[4] = (uint8_t)((entry >> 32) & 0xFF);
((uint8_t*)base)[5] = (uint8_t)((entry >> 40) & 0xFF);
((uint8_t*)base)[6] = (uint8_t)((entry >> 48) & 0xFF);
((uint8_t*)base)[7] = (uint8_t)((entry >> 56) & 0xFF);
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
}
void _KERNEL_ABI hal_write_pml4_entry(void *const base, uint64_t const pdpt_addr, uint64_t const attr)
{
if(base == NULL)
if (base == NULL)
return;
uint64_t const entry = (pdpt_addr & 0xFFFFFFFFFF000) | attr;
((uint8_t*)base)[0] = (uint8_t)(entry & 0xFF);
((uint8_t*)base)[1] = (uint8_t)((entry >> 8) & 0xFF);
((uint8_t*)base)[2] = (uint8_t)((entry >> 16) & 0xFF);
((uint8_t*)base)[3] = (uint8_t)((entry >> 24) & 0xFF);
((uint8_t*)base)[4] = (uint8_t)((entry >> 32) & 0xFF);
((uint8_t*)base)[5] = (uint8_t)((entry >> 40) & 0xFF);
((uint8_t*)base)[6] = (uint8_t)((entry >> 48) & 0xFF);
((uint8_t*)base)[7] = (uint8_t)((entry >> 56) & 0xFF);
((uint8_t *) base)[0] = (uint8_t) (entry & 0xFF);
((uint8_t *) base)[1] = (uint8_t) ((entry >> 8) & 0xFF);
((uint8_t *) base)[2] = (uint8_t) ((entry >> 16) & 0xFF);
((uint8_t *) base)[3] = (uint8_t) ((entry >> 24) & 0xFF);
((uint8_t *) base)[4] = (uint8_t) ((entry >> 32) & 0xFF);
((uint8_t *) base)[5] = (uint8_t) ((entry >> 40) & 0xFF);
((uint8_t *) base)[6] = (uint8_t) ((entry >> 48) & 0xFF);
((uint8_t *) base)[7] = (uint8_t) ((entry >> 56) & 0xFF);
return;
}
void _KERNEL_ABI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit, uint64_t const attr)
void _KERNEL_ABI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit,
uint64_t const attr)
{
if (gdt == NULL)
return;
uint64_t const seg_desc = (((uint64_t)base & 0xFFFF) << 16) | ((((uint64_t)base >> 16) & 0xFF) << 32) | ((((uint64_t)base >> 24) & 0xFF) << 56) | ((uint64_t)limit & 0xFFFF) | ((((uint64_t)limit >> 16) & 0xF) << 48) | attr;
((uint8_t*)gdt)[0] = (uint8_t)(seg_desc & 0xFF);
((uint8_t*)gdt)[1] = (uint8_t)((seg_desc >> 8) & 0xFF);
((uint8_t*)gdt)[2] = (uint8_t)((seg_desc >> 16) & 0xFF);
((uint8_t*)gdt)[3] = (uint8_t)((seg_desc >> 24) & 0xFF);
((uint8_t*)gdt)[4] = (uint8_t)((seg_desc >> 32) & 0xFF);
((uint8_t*)gdt)[5] = (uint8_t)((seg_desc >> 40) & 0xFF);
((uint8_t*)gdt)[6] = (uint8_t)((seg_desc >> 48) & 0xFF);
((uint8_t*)gdt)[7] = (uint8_t)((seg_desc >> 56) & 0xFF);
uint64_t const seg_desc = (((uint64_t) base & 0xFFFF) << 16) | ((((uint64_t) base >> 16) & 0xFF) << 32) |
((((uint64_t) base >> 24) & 0xFF) << 56) | ((uint64_t) limit & 0xFFFF) |
((((uint64_t) limit >> 16) & 0xF) << 48) | attr;
((uint8_t *) gdt)[0] = (uint8_t) (seg_desc & 0xFF);
((uint8_t *) gdt)[1] = (uint8_t) ((seg_desc >> 8) & 0xFF);
((uint8_t *) gdt)[2] = (uint8_t) ((seg_desc >> 16) & 0xFF);
((uint8_t *) gdt)[3] = (uint8_t) ((seg_desc >> 24) & 0xFF);
((uint8_t *) gdt)[4] = (uint8_t) ((seg_desc >> 32) & 0xFF);
((uint8_t *) gdt)[5] = (uint8_t) ((seg_desc >> 40) & 0xFF);
((uint8_t *) gdt)[6] = (uint8_t) ((seg_desc >> 48) & 0xFF);
((uint8_t *) gdt)[7] = (uint8_t) ((seg_desc >> 56) & 0xFF);
return;
}
void _KERNEL_ABI hal_create_initial_page_table(void* const base, uint64_t size)
void _KERNEL_ABI hal_create_initial_page_table(void *const base, uint64_t size)
{
};
@ -134,20 +137,19 @@ void _KERNEL_ABI hal_create_initial_page_table(void* const base, uint64_t size)
// return 0;
//}
void*_KERNEL_ABI hal_halloc(size_t const size)
void* _KERNEL_ABI hal_halloc(_IN size_t const size)
{
if(_cur_heap == NULL)
if (_cur_heap == NULL)
_cur_heap = kernel_heap;
if(_cur_heap + size < kernel_heap + kernel_heap_size)
if (_cur_heap + size < kernel_heap + kernel_heap_size)
{
void * temp_heap = (void*)_cur_heap;
_cur_heap = _cur_heap + size;
return temp_heap;
return _cur_heap - size;
}
return NULL;
}
void _KERNEL_ABI hal_hfree(void *ptr)
void _KERNEL_ABI hal_hfree(_IN void *ptr)
{
return;
}

View File

@ -86,9 +86,9 @@ typedef struct __attribute__((packed))
uint64_t edx;
} cpuid_t;
void*_KERNEL_ABI hal_halloc(size_t const size);
void* _KERNEL_ABI hal_halloc(_IN size_t const size);
void _KERNEL_ABI hal_hfree(void *ptr);
void _KERNEL_ABI hal_hfree(_IN void *ptr);
extern void _KERNEL_ABI hal_flush_gdt(gdt_ptr_t *gdt_ptr, uint64_t code_slct, uint64_t data_slct);