Figuring out PMM/VMM and ASM.

This commit is contained in:
secXsQuared 2016-06-24 18:47:29 -07:00
parent af7401a7ed
commit be746b4f6f
30 changed files with 124 additions and 69 deletions

View File

@ -3,12 +3,11 @@
* See COPYING under root for details
*/
#ifndef _S_DEF_H_
#define _S_DEF_H_
#ifndef _S_ABI_H_
#define _S_ABI_H_
#include <stddef.h>
#include <stdarg.h>
#include "s_type.h"
#define KAPI __attribute__((sysv_abi))
#define UAPI __attribute__((sysv_abi))

View File

@ -0,0 +1,42 @@
#ifndef _S_VMM_H_
#define _S_VMM_H_
#include "s_abi.h"
#include "s_type.h"
#define K_BASE_VADDR 0xFF
#define K_END_VADDR 0xFF
typedef struct
{
// the kernel always reserves this much virtual space for HAL
// this is mainly used for recursive page tables or other HAL actions
k_virtual_addr_t reserved_vaddr_base;
k_virtual_addr_t reserved_vaddr_end;
// the k_vaddr_alignment determines the alignment of the kernel's address space
// the reserved virtual address spaces above is also subject to the alignment
uint64_t k_vaddr_alignment;
} k_hal_vmm_info;
// this function always returns the physical address of the new address space
// the function should identity refer to the virtual address K_BASE_VADDR to K_END_VADDR according to the current address space
// so that these pages are global (modifying the mapping in this area affects everyone)
// the K_BASE_VADDR to K_END_VADDR includes the reserved virtual addr space by the HAL
// if HAL's reserved virtual addr will be mapped to different physical pages, the HAL should make the change
k_physical_addr_t KAPI k_create_address_space(k_physical_addr_t addr_space,
k_physical_page_alloc alloc);
// this function destroys the target address space without destroying the K_BASE_VADDR to K_END_VADDR
// target_addr_space is guaranteed to be not the same as the current address space
// when the function returns, the current address space must stay unchanged
void KAPI k_destroy_address_space(k_physical_addr_t target_addr_space,
k_physical_page_free free);
// as the name implies
void KAPI k_switch_address_space(k_physical_addr_t target_addr_space);
// as the name implies
k_physical_addr_t KAPI k_get_current_address_space();
#endif

View File

@ -1,6 +1,7 @@
#ifndef _S_ATOMIC_H_
#define _S_ATOMIC_H_
#include "s_def.h"
#include "s_abi.h"
#include "s_type.h"
extern uint64_t KAPI k_interlocked_exchange(uint64_t* target, uint64_t val);

View File

@ -1,6 +1,6 @@
#ifndef _S_BOOT_H_
#define _S_BOOT_H_
#include "s_def.h"
#include "s_abi.h"
//
// HAL Boot Info
//

View File

@ -1,17 +1,19 @@
#ifndef _S_CONTEXT_H_
#define _S_CONTEXT_H_
#include "s_def.h"
#include "s_vmm.h"
#include "s_abi.h"
#include "s_mm.h"
#include "s_intr.h"
// This function should never return and directly context switches to the target
// on x86, it should save the context, switch stack, build exc frame and iret
// This function always assumes interrupt context
extern void KAPI k_context_switch(void *intr_stack,
void *cur_context, void *next_context,
k_address_space_t old_addr_space, k_address_space_t next_addr_space);
k_physical_addr_t old_addr_space,
k_physical_addr_t next_addr_space);
extern void KAPI k_create_context(void *context, void *pc, void *sp);
extern void KAPI k_create_context(void *context, void* pc, void* sp, void* ksp, k_irql_t irql, void* arg);
extern void KAPI k_destroy_context(void* context);

View File

@ -1,20 +1,26 @@
#ifndef _S_INTR_H_
#define _S_INTR_H_
#include "s_def.h"
#include "s_abi.h"
typedef struct
{
uint32_t timer_intr_vec;
uint32_t apc_intr_vec;
uint32_t dpc_intr_vec;
uint32_t div_by_zero_exc_vec;
uint32_t general_protection_exc_vec;
uint32_t invalid_op_exc_vec;
uint32_t page_fault_exc_vec;
} k_hal_intr_info_t;
typedef enum
{
unrecoverable_exc,
div_by_zero_exc,
general_protection_exc,
invalid_op_exc,
page_fault_exc,
unsupported_exc,
debug_exc
} k_exc_type_t;
// IRQL APIs
typedef uint64_t k_irql_t;
#define K_IRQL_HIGH 4
@ -22,20 +28,27 @@ typedef uint64_t k_irql_t;
#define K_IRQL_DPC 2
#define K_IRQL_APC 1
#define K_IRQL_LOW 0
extern KAPI k_irql_t k_raise_irql(k_irql_t irql);
extern KAPI k_irql_t k_lower_irql(k_irql_t irql);
extern KAPI k_irql_t k_get_irql();
// 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 (*k_intr_handler_t)(void* context, void* intr_stack);
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 k_intr_handler_t KAPI k_deregister_intr_handler(uint32_t index);
// Exception handler registration
typedef void (*k_exc_handler_t)(uint64_t exc_addr, uint64_t exc_stack, uint64_t error_code);
extern void KAPI k_register_exc_handler(uint64_t index, k_exc_handler_t handler);
typedef void ( KAPI *k_exc_handler_t)(uint64_t exc_addr, uint64_t exc_stack, uint64_t error_code);
extern void KAPI k_register_exc_handler(k_exc_type_t type, k_exc_handler_t handler);
extern k_exc_handler_t KAPI k_deregister_exc_handler(uint64_t index);
#endif

View File

@ -0,0 +1,27 @@
#ifndef _S_PMM_H_
#define _S_PMM_H_
#include "s_abi.h"
#include "s_type.h"
#define K_PAGE_SIZE 4096
typedef uint64_t k_virtual_addr_t;
typedef uint64_t k_physical_addr_t;
//
// all the address spaces passed by the kernel would be initialized by k_create_address_space
// which means the kernel area/ as well as the HAL reserved vaddr ranges would be properly mapped
//
typedef k_physical_addr_t (KAPI *k_physical_page_alloc)(k_virtual_addr_t virtual_addr);
typedef void (KAPI *k_physical_page_free)(void *v_addr, k_physical_addr_t page);
// this function should map the v_addr to p_addr for the target address space
extern void KAPI k_map_virtual_addr(k_physical_addr_t addr_space,
k_virtual_addr_t v_addr,
k_physical_addr_t p_addr,
k_physical_page_alloc alloc);
#endif

View File

@ -1,31 +0,0 @@
#ifndef _S_MEM_H_
#define _S_MEM_H_
#include "s_def.h"
#define K_PAGE_SIZE 4096
typedef struct
{
// the kernel always reserves this much virtual space from the highest vaddr
// this is mainly used for recursive page tables
uint64_t reserved_v_addr_space;
} k_hal_vmm_info;
typedef void*(*k_map_virtual_addr_alloc)();
typedef uint64_t k_address_space_t;
// the alloc function returns the physical address of a page
// NULL = no available physical page
void k_map_virtual_addr(k_address_space_t addr_space, uint64_t v_addr, uint64_t p_addr, k_map_virtual_addr_alloc alloc);
// this function always returns the physical address (x86) or whatever makes sense to the HAL
// the whole virtual address should be unmapped except for the reserved virtual space.
// HAL maps it however it wants
k_address_space_t k_create_virtual_addr_space();
void k_destroy_virtual_addr_space(k_address_space_t addr_space);
// this function gives the context of a process and the HAL returns its address space
k_address_space_t k_get_addr_space(void* context);
#endif

View File

@ -6,7 +6,7 @@
#ifndef _LINKED_LIST_H_
#define _LINKED_LIST_H_
#include "s_def.h"
#include "s_abi.h"
typedef struct _linked_list_node_t
{

View File

@ -6,7 +6,7 @@
#ifndef _SALLOC_H_
#define _SALLOC_H_
#include "s_def.h"
#include "s_abi.h"
void KAPI salloc_init(void *base, uint32_t size);

View File

@ -3,7 +3,7 @@
* See COPYING under root for details
*/
#include "s_def.h"
#include "s_abi.h"
#include "bit_ops.h"
typedef union

View File

@ -7,7 +7,7 @@
#define _BIT_OPERATION_H_
#include "s_type.h"
#include "s_def.h"
#include "s_abi.h"
static inline uint64_t KAPI bit_mask(uint32_t bit)
{

View File

@ -6,7 +6,7 @@
#ifndef _STD_LIB_H_
#define _STD_LIB_H_
#include "s_def.h"
#include "s_abi.h"
#include "s_type.h"
uint32_t KAPI rand( void );

View File

@ -4,7 +4,7 @@
*/
#include "s_type.h"
#include "s_def.h"
#include "s_abi.h"
#include "std_lib.h"
void KAPI mem_cpy(void *src, void *dst, uint64_t size)

View File

@ -3,7 +3,7 @@
#include "s_boot.h"
#include "s_context.h"
#include "s_intr.h"
#include "s_vmm.h"
#include "s_asm.h"
uint64_t KAPI k_interlocked_exchange(uint64_t* target, uint64_t val)
{

View File

@ -3,7 +3,7 @@
* See COPYING under root for details
*/
#include "s_def.h"
#include "s_abi.h"
#include "s_type.h"
#include "hal_mem.h"
#include "salloc.h"

View File

@ -3,7 +3,7 @@
* See COPYING under root for details
*/
#include "s_def.h"
#include "s_abi.h"
#include "std_lib.h"
#include "hal_print.h"
#include "hal_var.h"

View File

@ -1,7 +1,7 @@
#ifndef _HAL_ARCH_H_
#define _HAL_ARCH_H_
#include "s_def.h"
#include "s_abi.h"
typedef struct
{

View File

@ -7,7 +7,7 @@
#define _HAL_IO_H_
#include "s_intr.h"
#include "s_def.h"
#include "s_abi.h"
#include "s_type.h"
#define GATE_DPL_0 (0ull << 13)

View File

@ -6,7 +6,7 @@
#ifndef _HAL_MEM_H_
#define _HAL_MEM_H_
#include "s_def.h"
#include "s_abi.h"
#include "s_type.h"
#include "linked_list.h"

View File

@ -5,7 +5,7 @@
#ifndef _HAL_PRINT_H_
#define _HAL_PRINT_H_
#include "s_def.h"
#include "s_abi.h"
#include "s_type.h"
#define get_column(pos) (pos % 80)

View File

@ -1,6 +1,6 @@
#ifndef _K_ALLOC_H_
#define _K_ALLOC_H_
#include "s_def.h"
#include "s_abi.h"
void KAPI k_alloc_init();

View File

@ -1,7 +1,7 @@
#ifndef _ATOMIC_H_
#define _ATOMIC_H_
#include "s_def.h"
#include "s_abi.h"
#include "k_intr.h"
typedef struct

View File

@ -3,7 +3,7 @@
#include "s_intr.h"
#include "s_type.h"
#include "s_def.h"
#include "s_abi.h"
typedef uint64_t k_irql_t;

View File

@ -0,0 +1,3 @@
#include "s_abi.h"
#include "s_mm.h"

View File

@ -1 +0,0 @@
#include "s_def.h"

View File

@ -1,4 +1,4 @@
#include "s_def.h"
#include "s_abi.h"
#include "salloc.h"
#define K_KERNEL_HEAP_SIZE 8192

View File

@ -1,4 +1,4 @@
#include "k_pmm.h"
#include "k_mm.h"
static uint64_t top_of_stack;

View File

@ -2,7 +2,7 @@
#define _K_TEST_DRIVER_H_
#include "s_type.h"
#include "s_def.h"
#include "s_abi.h"
void KAPI test_begin(char *name);