Squash commits for public release
This commit is contained in:
31
kernel/include/platform/x86/cpuinfo.h
Normal file
31
kernel/include/platform/x86/cpuinfo.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_CPUINFO_H
|
||||
#define _KERNEL_PLATFORM_X86_CPUINFO_H
|
||||
|
||||
#include <libkern/c_attrs.h>
|
||||
#include <libkern/types.h>
|
||||
#include <platform/generic/cpu.h>
|
||||
|
||||
enum CPUFEAT_FLAGS {
|
||||
CPUFEAT_FPU = (1 << 0),
|
||||
CPUFEAT_PSE = (1 << 1),
|
||||
CPUFEAT_PAE = (1 << 2),
|
||||
CPUFEAT_CLFSH = (1 << 3),
|
||||
CPUFEAT_SSE = (1 << 4),
|
||||
CPUFEAT_SSE2 = (1 << 5),
|
||||
CPUFEAT_SSE3 = (1 << 6),
|
||||
CPUFEAT_SSSE3 = (1 << 7),
|
||||
CPUFEAT_SSE4_1 = (1 << 8),
|
||||
CPUFEAT_SSE4_2 = (1 << 9),
|
||||
CPUFEAT_XSAVE = (1 << 10),
|
||||
CPUFEAT_AVX = (1 << 11),
|
||||
CPUFEAT_PDPE1GB = (1 << 12),
|
||||
};
|
||||
|
||||
void cpuinfo_init();
|
||||
|
||||
static inline bool cpuinfo_has_1gb_pages()
|
||||
{
|
||||
return TEST_FLAG(THIS_CPU->cpufeat, CPUFEAT_PDPE1GB);
|
||||
}
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_CPUINFO_H
|
||||
51
kernel/include/platform/x86/fpu/fpu.h
Normal file
51
kernel/include/platform/x86/fpu/fpu.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_FPU_FPU_H
|
||||
#define _KERNEL_PLATFORM_X86_FPU_FPU_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
#include <platform/x86/registers.h>
|
||||
|
||||
#define FPU_STATE_ALIGNMENT (16)
|
||||
|
||||
typedef struct {
|
||||
uint8_t buffer[512];
|
||||
#ifdef __x86_64__
|
||||
// Space for YMM.
|
||||
uint8_t ext_save_area[256];
|
||||
#endif
|
||||
} __attribute__((aligned(FPU_STATE_ALIGNMENT))) fpu_state_t;
|
||||
|
||||
void fpu_handler();
|
||||
void fpu_init();
|
||||
void fpu_init_state(fpu_state_t* new_fpu_state);
|
||||
|
||||
static inline void fpu_save(fpu_state_t* fpu_state)
|
||||
{
|
||||
asm volatile("fxsave %0"
|
||||
: "=m"(*fpu_state));
|
||||
}
|
||||
|
||||
static inline void fpu_restore(fpu_state_t* fpu_state)
|
||||
{
|
||||
asm volatile("fxrstor %0"
|
||||
:
|
||||
: "m"(*fpu_state));
|
||||
}
|
||||
|
||||
static inline int fpu_is_avail()
|
||||
{
|
||||
return (((read_cr0() >> 3) & 0b1) == 0b0);
|
||||
}
|
||||
|
||||
static inline void fpu_make_avail()
|
||||
{
|
||||
asm volatile("clts");
|
||||
}
|
||||
|
||||
static inline void fpu_make_unavail()
|
||||
{
|
||||
// Set TS bit of cr0 to 1.
|
||||
uintptr_t cr0 = read_cr0() | (1 << 3);
|
||||
write_cr0(cr0);
|
||||
}
|
||||
|
||||
#endif //_KERNEL_PLATFORM_X86_FPU_FPU_H
|
||||
125
kernel/include/platform/x86/gdt.h
Normal file
125
kernel/include/platform/x86/gdt.h
Normal file
@@ -0,0 +1,125 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_GDT_H
|
||||
#define _KERNEL_PLATFORM_X86_GDT_H
|
||||
|
||||
#include <libkern/c_attrs.h>
|
||||
#include <libkern/types.h>
|
||||
|
||||
#ifdef __x86_64__
|
||||
#define GDT_MAX_ENTRIES 7 // TSS takes 2 entries.
|
||||
|
||||
// Marking that code segment contains native 64-bit code.
|
||||
#define GDT_LONGMODE_FLAG 1
|
||||
#define GDT_DB_FLAG 0
|
||||
#else
|
||||
#define GDT_MAX_ENTRIES 6
|
||||
|
||||
#define GDT_LONGMODE_FLAG 0
|
||||
#define GDT_DB_FLAG 1
|
||||
#endif
|
||||
|
||||
#define GDT_SEG_NULL 0 // kernel code
|
||||
#define GDT_SEG_KCODE 1 // kernel code
|
||||
#define GDT_SEG_KDATA 2 // kernel data+stack
|
||||
#define GDT_SEG_UCODE 3 // user code
|
||||
#define GDT_SEG_UDATA 4 // user data+stack
|
||||
#define GDT_SEG_TSS 5 // task state NOT USED CURRENTLY
|
||||
|
||||
#define GDT_SEGF_X 0x8 // exec
|
||||
#define GDT_SEGF_A 0x1 // accessed
|
||||
#define GDT_SEGF_R 0x2 // readable (if exec)
|
||||
#define GDT_SEGF_C 0x4 // conforming seg (if exec)
|
||||
#define GDT_SEGF_W 0x2 // writeable (if non-exec)
|
||||
#define GDT_SEGF_D 0x4 // grows down (if non-exec)
|
||||
|
||||
#define FL_IF 0x00000200
|
||||
|
||||
#define DPL_KERN 0x0
|
||||
#define DPL_USER 0x3
|
||||
|
||||
struct PACKED gdt_desc {
|
||||
union {
|
||||
struct {
|
||||
uint32_t lim_15_0 : 16;
|
||||
uint32_t base_15_0 : 16;
|
||||
uint32_t base_23_16 : 8;
|
||||
uint32_t type : 4;
|
||||
uint32_t dt : 1;
|
||||
uint32_t dpl : 2;
|
||||
uint32_t p : 1;
|
||||
uint32_t lim_19_16 : 4;
|
||||
uint32_t avl : 1;
|
||||
uint32_t l : 1;
|
||||
uint32_t db : 1;
|
||||
uint32_t g : 1;
|
||||
uint32_t base_31_24 : 8;
|
||||
};
|
||||
uint32_t raw;
|
||||
};
|
||||
};
|
||||
typedef struct gdt_desc gdt_desc_t;
|
||||
|
||||
extern gdt_desc_t gdt[GDT_MAX_ENTRIES];
|
||||
|
||||
#define GDT_SEG_CODE_DESC(vtype, vbase, vlimit, vdpl) \
|
||||
(gdt_desc_t) \
|
||||
{ \
|
||||
.lim_15_0 = ((vlimit) >> 12) & 0xffff, \
|
||||
.base_15_0 = (uint32_t)(vbase)&0xffff, \
|
||||
.base_23_16 = ((uint32_t)(vbase) >> 16) & 0xff, \
|
||||
.type = vtype, \
|
||||
.dt = 1, \
|
||||
.dpl = vdpl, \
|
||||
.p = 1, \
|
||||
.lim_19_16 = ((uint32_t)(vlimit) >> 28), \
|
||||
.avl = 0, \
|
||||
.l = GDT_LONGMODE_FLAG, \
|
||||
.db = GDT_DB_FLAG, \
|
||||
.g = 1, \
|
||||
.base_31_24 = (uint32_t)(vbase) >> 24 \
|
||||
}
|
||||
|
||||
#define GDT_SEG_DATA_DESC(vtype, vbase, vlimit, vdpl) \
|
||||
(gdt_desc_t) \
|
||||
{ \
|
||||
.lim_15_0 = ((vlimit) >> 12) & 0xffff, \
|
||||
.base_15_0 = (uint32_t)(vbase)&0xffff, \
|
||||
.base_23_16 = ((uint32_t)(vbase) >> 16) & 0xff, \
|
||||
.type = vtype, \
|
||||
.dt = 1, \
|
||||
.dpl = vdpl, \
|
||||
.p = 1, \
|
||||
.lim_19_16 = ((uint32_t)(vlimit) >> 28), \
|
||||
.avl = 0, \
|
||||
.l = 0, \
|
||||
.db = 1, \
|
||||
.g = 1, \
|
||||
.base_31_24 = (uint32_t)(vbase) >> 24 \
|
||||
}
|
||||
|
||||
#define GDT_SEG_TSS_DESC(vtype, vbase, vlimit, vdpl) \
|
||||
(gdt_desc_t) \
|
||||
{ \
|
||||
.lim_15_0 = ((vlimit)) & 0xffff, \
|
||||
.base_15_0 = (uint32_t)(vbase)&0xffff, \
|
||||
.base_23_16 = ((uint32_t)(vbase) >> 16) & 0xff, \
|
||||
.type = vtype, \
|
||||
.dt = 0, \
|
||||
.dpl = vdpl, \
|
||||
.p = 1, \
|
||||
.lim_19_16 = ((uint32_t)(vlimit) >> 16), \
|
||||
.avl = 0, \
|
||||
.l = 0, \
|
||||
.db = 0, \
|
||||
.g = 0, \
|
||||
.base_31_24 = (uint32_t)(vbase) >> 24 \
|
||||
}
|
||||
|
||||
#define GDT_SEG_SET_RAW(rawvalue) \
|
||||
(gdt_desc_t) \
|
||||
{ \
|
||||
.raw = rawvalue \
|
||||
}
|
||||
|
||||
void gdt_setup();
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_GDT_H
|
||||
26
kernel/include/platform/x86/i386/tasking/context.h
Normal file
26
kernel/include/platform/x86/i386/tasking/context.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_I386_TASKING_CONTEXT_H
|
||||
#define _KERNEL_PLATFORM_X86_I386_TASKING_CONTEXT_H
|
||||
|
||||
#include <libkern/c_attrs.h>
|
||||
#include <libkern/types.h>
|
||||
|
||||
struct PACKED context {
|
||||
uint32_t edi;
|
||||
uint32_t esi;
|
||||
uint32_t ebx;
|
||||
uint32_t ebp;
|
||||
uint32_t eip;
|
||||
};
|
||||
typedef struct context context_t;
|
||||
|
||||
static inline uintptr_t context_get_instruction_pointer(context_t* ctx)
|
||||
{
|
||||
return ctx->eip;
|
||||
}
|
||||
|
||||
static inline void context_set_instruction_pointer(context_t* ctx, uintptr_t ip)
|
||||
{
|
||||
ctx->eip = ip;
|
||||
}
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_I386_TASKING_CONTEXT_H
|
||||
125
kernel/include/platform/x86/i386/tasking/trapframe.h
Normal file
125
kernel/include/platform/x86/i386/tasking/trapframe.h
Normal file
@@ -0,0 +1,125 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_I386_TASKING_TRAPFRAME_H
|
||||
#define _KERNEL_PLATFORM_X86_I386_TASKING_TRAPFRAME_H
|
||||
|
||||
#include <libkern/c_attrs.h>
|
||||
#include <libkern/types.h>
|
||||
#include <platform/x86/gdt.h>
|
||||
#include <platform/x86/tasking/tss.h>
|
||||
|
||||
struct PACKED trapframe {
|
||||
// registers as pushed by pusha
|
||||
uint32_t edi;
|
||||
uint32_t esi;
|
||||
uint32_t ebp;
|
||||
uint32_t oesp; // useless & ignored
|
||||
uint32_t ebx;
|
||||
uint32_t edx;
|
||||
uint32_t ecx;
|
||||
uint32_t eax;
|
||||
|
||||
// rest of trap frame
|
||||
uint16_t gs;
|
||||
uint16_t padding1;
|
||||
uint16_t fs;
|
||||
uint16_t padding2;
|
||||
uint16_t es;
|
||||
uint16_t padding3;
|
||||
uint16_t ds;
|
||||
uint16_t padding4;
|
||||
uint32_t int_no;
|
||||
|
||||
// below here defined by x86 hardware
|
||||
uint32_t err;
|
||||
uint32_t eip;
|
||||
uint16_t cs;
|
||||
uint16_t padding5;
|
||||
uint32_t eflags;
|
||||
|
||||
// below here only when crossing rings, such as from user to kernel
|
||||
uint32_t esp;
|
||||
uint16_t ss;
|
||||
uint16_t padding6;
|
||||
};
|
||||
typedef struct trapframe trapframe_t;
|
||||
|
||||
static inline uintptr_t get_stack_pointer(trapframe_t* tf)
|
||||
{
|
||||
return tf->esp;
|
||||
}
|
||||
|
||||
static inline void set_stack_pointer(trapframe_t* tf, uintptr_t sp)
|
||||
{
|
||||
tf->esp = sp;
|
||||
}
|
||||
|
||||
static inline uintptr_t get_frame_pointer(trapframe_t* tf)
|
||||
{
|
||||
return tf->ebp;
|
||||
}
|
||||
|
||||
static inline void set_frame_pointer(trapframe_t* tf, uintptr_t bp)
|
||||
{
|
||||
tf->ebp = bp;
|
||||
}
|
||||
|
||||
static inline uintptr_t get_instruction_pointer(trapframe_t* tf)
|
||||
{
|
||||
return tf->eip;
|
||||
}
|
||||
|
||||
static inline void set_instruction_pointer(trapframe_t* tf, uintptr_t ip)
|
||||
{
|
||||
tf->eip = ip;
|
||||
}
|
||||
|
||||
static inline uintptr_t get_syscall_result(trapframe_t* tf)
|
||||
{
|
||||
return tf->eax;
|
||||
}
|
||||
|
||||
static inline void set_syscall_result(trapframe_t* tf, uintptr_t val)
|
||||
{
|
||||
tf->eax = val;
|
||||
}
|
||||
|
||||
/**
|
||||
* STACK FUNCTIONS
|
||||
*/
|
||||
|
||||
static inline void tf_push_to_stack(trapframe_t* tf, uintptr_t val)
|
||||
{
|
||||
tf->esp -= sizeof(uintptr_t);
|
||||
*((uintptr_t*)tf->esp) = val;
|
||||
}
|
||||
|
||||
static inline uint32_t tf_pop_to_stack(trapframe_t* tf)
|
||||
{
|
||||
uintptr_t val = *((uintptr_t*)tf->esp);
|
||||
tf->esp += sizeof(uintptr_t);
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline void tf_move_stack_pointer(trapframe_t* tf, int32_t val)
|
||||
{
|
||||
tf->esp += val;
|
||||
}
|
||||
|
||||
static inline void tf_setup_as_user_thread(trapframe_t* tf)
|
||||
{
|
||||
tf->cs = (GDT_SEG_UCODE << 3) | DPL_USER;
|
||||
tf->ds = (GDT_SEG_UDATA << 3) | DPL_USER;
|
||||
tf->es = tf->ds;
|
||||
tf->ss = tf->ds;
|
||||
tf->eflags = FL_IF;
|
||||
}
|
||||
|
||||
static inline void tf_setup_as_kernel_thread(trapframe_t* tf)
|
||||
{
|
||||
tf->cs = (GDT_SEG_KCODE << 3);
|
||||
tf->ds = (GDT_SEG_KDATA << 3);
|
||||
tf->es = tf->ds;
|
||||
tf->ss = tf->ds;
|
||||
tf->eflags = FL_IF;
|
||||
}
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_I386_TASKING_TRAPFRAME_H
|
||||
29
kernel/include/platform/x86/i386/vmm/consts.h
Normal file
29
kernel/include/platform/x86/i386/vmm/consts.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_I386_VMM_CONSTS_H
|
||||
#define _KERNEL_PLATFORM_X86_I386_VMM_CONSTS_H
|
||||
|
||||
static inline int vm_page_mask()
|
||||
{
|
||||
return 0xfff;
|
||||
}
|
||||
|
||||
#define VMM_LV0_ENTITY_COUNT (1024)
|
||||
#define VMM_LV1_ENTITY_COUNT (1024)
|
||||
#define VMM_PAGE_SIZE (4096)
|
||||
|
||||
#define VMM_OFFSET_IN_DIRECTORY(a) (((a) >> 22) & 0x3ff)
|
||||
#define VMM_OFFSET_IN_TABLE(a) (((a) >> 12) & 0x3ff)
|
||||
#define VMM_OFFSET_IN_PAGE(a) ((a)&0xfff)
|
||||
#define TABLE_START(vaddr) ((vaddr >> 22) << 22)
|
||||
#define PAGE_START(vaddr) ((vaddr >> 12) << 12)
|
||||
#define FRAME(addr) (addr / VMM_PAGE_SIZE)
|
||||
|
||||
#define PTABLE_TOP_KERNEL_OFFSET 768
|
||||
|
||||
#define PTABLE_LV_TOP (1)
|
||||
#define PTABLE_LV0_VADDR_OFFSET (12)
|
||||
#define PTABLE_LV1_VADDR_OFFSET (22)
|
||||
|
||||
#define USER_HIGH 0xbfffffff
|
||||
#define KERNEL_BASE 0xc0000000
|
||||
|
||||
#endif //_KERNEL_PLATFORM_X86_I386_VMM_CONSTS_H
|
||||
29
kernel/include/platform/x86/i386/vmm/mmu.h
Normal file
29
kernel/include/platform/x86/i386/vmm/mmu.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_I386_VMM_MMU_H
|
||||
#define _KERNEL_PLATFORM_X86_I386_VMM_MMU_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
#include <mem/bits/mmu.h>
|
||||
#include <platform/x86/i386/vmm/pde.h>
|
||||
#include <platform/x86/i386/vmm/pte.h>
|
||||
|
||||
typedef uint32_t ptable_entity_t;
|
||||
typedef uint32_t arch_pf_info_t;
|
||||
|
||||
ptable_entity_t vm_mmu_to_arch_flags(mmu_flags_t mmu_flags, ptable_lv_t lv);
|
||||
mmu_flags_t vm_arch_to_mmu_flags(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
mmu_pf_info_flags_t vm_arch_parse_pf_info(arch_pf_info_t info);
|
||||
|
||||
void vm_ptable_entity_set_default_flags(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
void vm_ptable_entity_allocated(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
void vm_ptable_entity_invalidate(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
|
||||
void vm_ptable_entity_set_mmu_flags(ptable_entity_t* entity, ptable_lv_t lv, mmu_flags_t mmu_flags);
|
||||
void vm_ptable_entity_rm_mmu_flags(ptable_entity_t* entity, ptable_lv_t lv, mmu_flags_t mmu_flags);
|
||||
void vm_ptable_entity_set_frame(ptable_entity_t* entity, ptable_lv_t lv, uintptr_t frame);
|
||||
uintptr_t vm_ptable_entity_get_frame(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
|
||||
ptable_state_t vm_ptable_entity_state(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
bool vm_ptable_entity_is_present(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
bool vm_ptable_entity_is_only_allocated(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_I386_VMM_MMU_H
|
||||
25
kernel/include/platform/x86/i386/vmm/pde.h
Normal file
25
kernel/include/platform/x86/i386/vmm/pde.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_I386_VMM_PDE_H
|
||||
#define _KERNEL_PLATFORM_X86_I386_VMM_PDE_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
|
||||
#define table_desc_t uint32_t
|
||||
#define pde_t uint32_t
|
||||
|
||||
#define TABLE_DESC_FRAME_OFFSET 12
|
||||
|
||||
enum TABLE_DESC_PAGE_FLAGS {
|
||||
TABLE_DESC_PRESENT = 0x1,
|
||||
TABLE_DESC_WRITABLE = 0x2,
|
||||
TABLE_DESC_USER = 0x4,
|
||||
TABLE_DESC_PWT = 0x8,
|
||||
TABLE_DESC_PCD = 0x10,
|
||||
TABLE_DESC_ACCESSED = 0x20,
|
||||
TABLE_DESC_DIRTY = 0x40,
|
||||
TABLE_DESC_4MB = 0x80,
|
||||
TABLE_DESC_CPU_GLOBAL = 0x100,
|
||||
TABLE_DESC_LV4_GLOBAL = 0x200,
|
||||
TABLE_DESC_COPY_ON_WRITE = 0x400,
|
||||
};
|
||||
|
||||
#endif //_KERNEL_PLATFORM_X86_I386_VMM_PDE_H
|
||||
23
kernel/include/platform/x86/i386/vmm/pte.h
Normal file
23
kernel/include/platform/x86/i386/vmm/pte.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_I386_VMM_PTE_H
|
||||
#define _KERNEL_PLATFORM_X86_I386_VMM_PTE_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
#include <mem/bits/mmu.h>
|
||||
|
||||
#define PAGE_DESC_FRAME_OFFSET 12
|
||||
|
||||
enum PAGE_DESC_PAGE_FLAGS {
|
||||
PAGE_DESC_PRESENT = 0x1,
|
||||
PAGE_DESC_WRITABLE = 0x2,
|
||||
PAGE_DESC_USER = 0x4,
|
||||
PAGE_DESC_WRITETHOUGH = 0x8,
|
||||
PAGE_DESC_NOT_CACHEABLE = 0x10,
|
||||
PAGE_DESC_ACCESSED = 0x20,
|
||||
PAGE_DESC_DIRTY = 0x40,
|
||||
PAGE_DESC_PAT = 0x80,
|
||||
PAGE_DESC_CPU_GLOBAL = 0x100,
|
||||
PAGE_DESC_LV4_GLOBAL = 0x200,
|
||||
PAGE_DESC_COPY_ON_WRITE = 0x400,
|
||||
};
|
||||
|
||||
#endif //_KERNEL_PLATFORM_X86_I386_VMM_PTE_H
|
||||
103
kernel/include/platform/x86/idt.h
Normal file
103
kernel/include/platform/x86/idt.h
Normal file
@@ -0,0 +1,103 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_IDT_H
|
||||
#define _KERNEL_PLATFORM_X86_IDT_H
|
||||
|
||||
#include <drivers/irq/irq_api.h>
|
||||
#include <drivers/irq/x86/pic.h>
|
||||
#include <libkern/c_attrs.h>
|
||||
#include <libkern/types.h>
|
||||
#include <platform/x86/port.h>
|
||||
#include <platform/x86/tasking/trapframe.h>
|
||||
|
||||
#define IDT_ENTRIES 256
|
||||
|
||||
#define IRQ_MASTER_OFFSET 32
|
||||
#define IRQ_SLAVE_OFFSET 40
|
||||
|
||||
struct PACKED idt_entry {
|
||||
uint16_t offset_lower; // bits 0..15
|
||||
uint16_t segment;
|
||||
uint8_t zero;
|
||||
uint8_t type;
|
||||
uint16_t offset_upper; // bits 16..31
|
||||
#ifdef __x86_64__
|
||||
uint32_t offset_long; // bits 32..63
|
||||
uint32_t zero2;
|
||||
#endif
|
||||
};
|
||||
typedef struct idt_entry idt_entry_t;
|
||||
|
||||
void interrupts_setup();
|
||||
|
||||
/* ISRs reserved for CPU exceptions */
|
||||
extern void isr0();
|
||||
extern void isr1();
|
||||
extern void isr2();
|
||||
extern void isr3();
|
||||
extern void isr4();
|
||||
extern void isr5();
|
||||
extern void isr6();
|
||||
extern void isr7();
|
||||
extern void isr8();
|
||||
extern void isr9();
|
||||
extern void isr10();
|
||||
extern void isr11();
|
||||
extern void isr12();
|
||||
extern void isr13();
|
||||
extern void isr14();
|
||||
extern void isr15();
|
||||
extern void isr16();
|
||||
extern void isr17();
|
||||
extern void isr18();
|
||||
extern void isr19();
|
||||
extern void isr20();
|
||||
extern void isr21();
|
||||
extern void isr22();
|
||||
extern void isr23();
|
||||
extern void isr24();
|
||||
extern void isr25();
|
||||
extern void isr26();
|
||||
extern void isr27();
|
||||
extern void isr28();
|
||||
extern void isr29();
|
||||
extern void isr30();
|
||||
extern void isr31();
|
||||
/* IRQ definitions */
|
||||
extern void irq0();
|
||||
extern void irq1();
|
||||
extern void irq2();
|
||||
extern void irq3();
|
||||
extern void irq4();
|
||||
extern void irq5();
|
||||
extern void irq6();
|
||||
extern void irq7();
|
||||
extern void irq8();
|
||||
extern void irq9();
|
||||
extern void irq10();
|
||||
extern void irq11();
|
||||
extern void irq12();
|
||||
extern void irq13();
|
||||
extern void irq14();
|
||||
extern void irq15();
|
||||
extern void irq_null();
|
||||
extern void irq_empty_handler();
|
||||
|
||||
extern void syscall();
|
||||
|
||||
#define IRQ0 32
|
||||
#define IRQ1 33
|
||||
#define IRQ2 34
|
||||
#define IRQ3 35
|
||||
#define IRQ4 36
|
||||
#define IRQ5 37
|
||||
#define IRQ6 38
|
||||
#define IRQ7 39
|
||||
#define IRQ8 40
|
||||
#define IRQ9 41
|
||||
#define IRQ10 42
|
||||
#define IRQ11 43
|
||||
#define IRQ12 44
|
||||
#define IRQ13 45
|
||||
#define IRQ14 46
|
||||
#define IRQ15 47
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_IDT_H
|
||||
10
kernel/include/platform/x86/init.h
Normal file
10
kernel/include/platform/x86/init.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_INIT_H
|
||||
#define _KERNEL_PLATFORM_X86_INIT_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
|
||||
void platform_init_boot_cpu();
|
||||
void platform_setup_boot_cpu();
|
||||
void platform_setup_secondary_cpu();
|
||||
|
||||
#endif /* _KERNEL_PLATFORM_X86_INIT_H */
|
||||
9
kernel/include/platform/x86/irq_handler.h
Normal file
9
kernel/include/platform/x86/irq_handler.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_IRQ_HANDLER_H
|
||||
#define _KERNEL_PLATFORM_X86_IRQ_HANDLER_H
|
||||
|
||||
#include <platform/x86/idt.h>
|
||||
|
||||
void irq_handler(trapframe_t* tf);
|
||||
void irq_empty_handler();
|
||||
|
||||
#endif
|
||||
9
kernel/include/platform/x86/isr_handler.h
Normal file
9
kernel/include/platform/x86/isr_handler.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_ISR_HANDLER_H
|
||||
#define _KERNEL_PLATFORM_X86_ISR_HANDLER_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
#include <platform/x86/idt.h>
|
||||
|
||||
void isr_handler(trapframe_t* tf);
|
||||
|
||||
#endif
|
||||
8
kernel/include/platform/x86/pmm/settings.h
Normal file
8
kernel/include/platform/x86/pmm/settings.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_PMM_SETTINGS_H
|
||||
#define _KERNEL_PLATFORM_X86_PMM_SETTINGS_H
|
||||
|
||||
#define PMM_BLOCK_SIZE (4096)
|
||||
#define PMM_BLOCK_SIZE_KB (4)
|
||||
#define PMM_BLOCKS_PER_BYTE (8)
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_PMM_SETTINGS_H
|
||||
20
kernel/include/platform/x86/port.h
Normal file
20
kernel/include/platform/x86/port.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_PORT_H
|
||||
#define _KERNEL_PLATFORM_X86_PORT_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
|
||||
uint8_t port_read8(uint16_t port);
|
||||
void port_write8(uint16_t port, uint8_t data);
|
||||
uint16_t port_read16(uint16_t port);
|
||||
void port_write16(uint16_t port, uint16_t data);
|
||||
uint32_t port_read32(uint16_t port);
|
||||
void port_write32(uint16_t port, uint32_t data);
|
||||
uint8_t port_read8(uint16_t port);
|
||||
void port_write8(uint16_t port, uint8_t data);
|
||||
uint16_t port_read16(uint16_t port);
|
||||
void port_write16(uint16_t port, uint16_t data);
|
||||
uint32_t port_read32(uint16_t port);
|
||||
void port_write32(uint16_t port, uint32_t data);
|
||||
void port_wait_io();
|
||||
|
||||
#endif
|
||||
65
kernel/include/platform/x86/registers.h
Normal file
65
kernel/include/platform/x86/registers.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_REGISTERS_H
|
||||
#define _KERNEL_PLATFORM_X86_REGISTERS_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
|
||||
extern uintptr_t read_ip();
|
||||
static inline uintptr_t read_cr2()
|
||||
{
|
||||
uintptr_t val;
|
||||
asm volatile("mov %%cr2, %0"
|
||||
: "=r"(val));
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline uintptr_t read_cr3()
|
||||
{
|
||||
uintptr_t val;
|
||||
asm volatile("mov %%cr3, %0"
|
||||
: "=r"(val));
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline uintptr_t read_sp()
|
||||
{
|
||||
uintptr_t val;
|
||||
#ifdef BITS32
|
||||
asm volatile("mov %%esp, %0"
|
||||
: "=r"(val));
|
||||
#else
|
||||
asm volatile("mov %%rsp, %0"
|
||||
: "=r"(val));
|
||||
#endif
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline uintptr_t read_bp()
|
||||
{
|
||||
uintptr_t val;
|
||||
#ifdef BITS32
|
||||
asm volatile("mov %%ebp, %0"
|
||||
: "=r"(val));
|
||||
#else
|
||||
asm volatile("mov %%rbp, %0"
|
||||
: "=r"(val));
|
||||
#endif
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline uintptr_t read_cr0()
|
||||
{
|
||||
uintptr_t val;
|
||||
asm volatile("mov %%cr0, %0"
|
||||
: "=r"(val));
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline void write_cr0(uintptr_t val)
|
||||
{
|
||||
asm volatile("mov %0, %%cr0"
|
||||
:
|
||||
: "r"(val)
|
||||
: "memory");
|
||||
}
|
||||
|
||||
#endif /* _KERNEL_PLATFORM_X86_REGISTERS_H */
|
||||
32
kernel/include/platform/x86/syscalls/params.h
Normal file
32
kernel/include/platform/x86/syscalls/params.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_SYSCALLS_PARAMS_H
|
||||
#define _KERNEL_PLATFORM_X86_SYSCALLS_PARAMS_H
|
||||
|
||||
#include <platform/x86/tasking/trapframe.h>
|
||||
|
||||
#define SYSCALL_HANDLER_NO 0x80
|
||||
|
||||
#ifdef __i386__
|
||||
#define SYSCALL_ID(tf) (tf->eax)
|
||||
#define SYSCALL_VAR1(tf) (tf->ebx)
|
||||
#define SYSCALL_VAR2(tf) (tf->ecx)
|
||||
#define SYSCALL_VAR3(tf) (tf->edx)
|
||||
#define SYSCALL_VAR4(tf) (tf->esi)
|
||||
#define SYSCALL_VAR5(tf) (tf->edi)
|
||||
#define return_val (tf->eax)
|
||||
#define return_with_val(val) \
|
||||
(return_val = val); \
|
||||
return
|
||||
#elif __x86_64__
|
||||
#define SYSCALL_ID(tf) (tf->rax)
|
||||
#define SYSCALL_VAR1(tf) (tf->rdi)
|
||||
#define SYSCALL_VAR2(tf) (tf->rsi)
|
||||
#define SYSCALL_VAR3(tf) (tf->rdx)
|
||||
#define SYSCALL_VAR4(tf) (tf->r10)
|
||||
#define SYSCALL_VAR5(tf) (tf->r8)
|
||||
#define return_val (tf->rax)
|
||||
#define return_with_val(val) \
|
||||
(return_val = val); \
|
||||
return
|
||||
#endif
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_SYSCALLS_PARAMS_H
|
||||
100
kernel/include/platform/x86/system.h
Normal file
100
kernel/include/platform/x86/system.h
Normal file
@@ -0,0 +1,100 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_SYSTEM_H
|
||||
#define _KERNEL_PLATFORM_X86_SYSTEM_H
|
||||
|
||||
#include <libkern/c_attrs.h>
|
||||
#include <libkern/types.h>
|
||||
#include <platform/generic/registers.h>
|
||||
|
||||
/**
|
||||
* INTS
|
||||
*/
|
||||
|
||||
void system_disable_interrupts();
|
||||
void system_enable_interrupts();
|
||||
void system_enable_interrupts_only_counter();
|
||||
inline static void system_disable_interrupts_no_counter() { asm volatile("cli"); }
|
||||
inline static void system_enable_interrupts_no_counter() { asm volatile("sti"); }
|
||||
|
||||
/**
|
||||
* PAGING
|
||||
*/
|
||||
|
||||
inline static void system_set_pdir(uintptr_t pdir0, uintptr_t pdir1)
|
||||
{
|
||||
asm volatile("mov %0, %%cr3"
|
||||
:
|
||||
: "r"(pdir0)
|
||||
: "memory");
|
||||
}
|
||||
|
||||
inline static void system_flush_local_tlb_entry(uintptr_t vaddr)
|
||||
{
|
||||
asm volatile("invlpg (%0)" ::"r"(vaddr)
|
||||
: "memory");
|
||||
}
|
||||
|
||||
inline static void system_flush_all_cpus_tlb_entry(uintptr_t vaddr)
|
||||
{
|
||||
system_flush_local_tlb_entry(vaddr);
|
||||
// TODO: Send inter-processor messages.
|
||||
}
|
||||
|
||||
inline static void system_flush_whole_tlb()
|
||||
{
|
||||
system_set_pdir(read_cr3(), 0x0);
|
||||
}
|
||||
|
||||
inline static void system_enable_write_protect()
|
||||
{
|
||||
uintptr_t cr = read_cr0();
|
||||
cr |= 0x10000;
|
||||
write_cr0(cr);
|
||||
}
|
||||
|
||||
inline static void system_disable_write_protect()
|
||||
{
|
||||
uintptr_t cr = read_cr0();
|
||||
cr &= 0xfffeffff;
|
||||
write_cr0(cr);
|
||||
}
|
||||
|
||||
inline static void system_enable_paging()
|
||||
{
|
||||
uintptr_t cr = read_cr0();
|
||||
cr |= 0x80000000;
|
||||
write_cr0(cr);
|
||||
}
|
||||
|
||||
inline static void system_disable_paging()
|
||||
{
|
||||
uintptr_t cr = read_cr0();
|
||||
cr &= 0x7fffffff;
|
||||
write_cr0(cr);
|
||||
}
|
||||
|
||||
inline static void system_stop_until_interrupt()
|
||||
{
|
||||
asm volatile("hlt");
|
||||
}
|
||||
|
||||
NORETURN inline static void system_stop()
|
||||
{
|
||||
system_disable_interrupts();
|
||||
system_stop_until_interrupt();
|
||||
while (1) { }
|
||||
}
|
||||
|
||||
/**
|
||||
* CPU
|
||||
*/
|
||||
|
||||
void system_cache_clean_and_invalidate(void* addr, size_t size);
|
||||
void system_cache_invalidate(void* addr, size_t size);
|
||||
void system_cache_clean(void* addr, size_t size);
|
||||
|
||||
inline static int system_cpu_id()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* _KERNEL_PLATFORM_X86_SYSTEM_H */
|
||||
5
kernel/include/platform/x86/tasking/context.h
Normal file
5
kernel/include/platform/x86/tasking/context.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#ifdef __i386__
|
||||
#include <platform/x86/i386/tasking/context.h>
|
||||
#elif __x86_64__
|
||||
#include <platform/x86/x86_64/tasking/context.h>
|
||||
#endif
|
||||
12
kernel/include/platform/x86/tasking/dump_impl.h
Normal file
12
kernel/include/platform/x86/tasking/dump_impl.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_TASKING_DUMP_IMPL_H
|
||||
#define _KERNEL_PLATFORM_X86_TASKING_DUMP_IMPL_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
#include <tasking/bits/dump.h>
|
||||
#include <tasking/tasking.h>
|
||||
|
||||
int dump_impl(dump_data_t* data);
|
||||
int dump_kernel_impl(dump_data_t* dump_data, const char* err_desc);
|
||||
int dump_kernel_impl_from_tf(dump_data_t* dump_data, const char* err_desc, trapframe_t* tf);
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_TASKING_DUMP_IMPL_H
|
||||
10
kernel/include/platform/x86/tasking/signal_impl.h
Normal file
10
kernel/include/platform/x86/tasking/signal_impl.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_TASKING_SIGNAL_IMPL_H
|
||||
#define _KERNEL_PLATFORM_X86_TASKING_SIGNAL_IMPL_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
struct thread;
|
||||
|
||||
int signal_impl_prepare_stack(struct thread* thread, int signo, uintptr_t old_sp, uintptr_t magic);
|
||||
int signal_impl_restore_stack(struct thread* thread, uintptr_t* old_sp, uintptr_t* magic);
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_TASKING_SIGNAL_IMPL_H
|
||||
9
kernel/include/platform/x86/tasking/switchvm.h
Normal file
9
kernel/include/platform/x86/tasking/switchvm.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_TASKING_SWITCHVM_H
|
||||
#define _KERNEL_PLATFORM_X86_TASKING_SWITCHVM_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
#include <tasking/tasking.h>
|
||||
|
||||
void switch_uthreads(thread_t* thread);
|
||||
|
||||
#endif
|
||||
5
kernel/include/platform/x86/tasking/trapframe.h
Normal file
5
kernel/include/platform/x86/tasking/trapframe.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#ifdef __i386__
|
||||
#include <platform/x86/i386/tasking/trapframe.h>
|
||||
#elif __x86_64__
|
||||
#include <platform/x86/x86_64/tasking/trapframe.h>
|
||||
#endif
|
||||
76
kernel/include/platform/x86/tasking/tss.h
Normal file
76
kernel/include/platform/x86/tasking/tss.h
Normal file
@@ -0,0 +1,76 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_TASKING_TSS_H
|
||||
#define _KERNEL_PLATFORM_X86_TASKING_TSS_H
|
||||
|
||||
#include <libkern/c_attrs.h>
|
||||
#include <libkern/types.h>
|
||||
|
||||
#define SEGTSS_TYPE 0x9 // defined in the Intel's manual 3a
|
||||
|
||||
#ifdef __i386__
|
||||
struct PACKED tss {
|
||||
uint32_t back_link : 16; // back link to prev tss
|
||||
uint32_t zero1 : 16; // always zero
|
||||
uint32_t esp0 : 32; // stack pointer at ring 0
|
||||
uint32_t ss0 : 16; // stack segment at ring 0
|
||||
uint32_t zero2 : 16; // always zero
|
||||
uint32_t esp1 : 32; // stack pointer at ring 1
|
||||
uint32_t ss1 : 16; // stack segment at ring 1
|
||||
uint32_t zero3 : 16; // always zero
|
||||
uint32_t esp2 : 32; // stack pointer at ring 2
|
||||
uint32_t ss2 : 16; // stack segment at ring 2
|
||||
uint32_t zero4 : 16; // always zero
|
||||
uint32_t cr3 : 32;
|
||||
uint32_t eip : 32;
|
||||
uint32_t eflag : 32;
|
||||
uint32_t eax : 32;
|
||||
uint32_t ecx : 32;
|
||||
uint32_t edx : 32;
|
||||
uint32_t ebx : 32;
|
||||
uint32_t esp : 32;
|
||||
uint32_t ebp : 32;
|
||||
uint32_t esi : 32;
|
||||
uint32_t edi : 32;
|
||||
uint32_t es : 16;
|
||||
uint32_t zero5 : 16; // always zero
|
||||
uint32_t cs : 16;
|
||||
uint32_t zero6 : 16; // always zero
|
||||
uint32_t ss : 16;
|
||||
uint32_t zero7 : 16; // always zero
|
||||
uint32_t ds : 16;
|
||||
uint32_t zero8 : 16; // always zero
|
||||
uint32_t fs : 16;
|
||||
uint32_t zero9 : 16; // always zero
|
||||
uint32_t gs : 16;
|
||||
uint32_t zero10 : 16; // always zero
|
||||
uint32_t ldt_selector : 16;
|
||||
uint32_t zero11 : 16; // always zero
|
||||
uint32_t t : 1;
|
||||
uint32_t zero12 : 15; // always zero
|
||||
uint32_t iomap_offset : 16;
|
||||
};
|
||||
#elif __x86_64__
|
||||
struct PACKED tss {
|
||||
uint32_t res1;
|
||||
uint64_t rsp0;
|
||||
uint64_t rsp1;
|
||||
uint64_t rsp2;
|
||||
uint64_t res2;
|
||||
uint64_t ist1;
|
||||
uint64_t ist2;
|
||||
uint64_t ist3;
|
||||
uint64_t ist4;
|
||||
uint64_t ist5;
|
||||
uint64_t ist6;
|
||||
uint64_t ist7;
|
||||
uint64_t res3;
|
||||
uint16_t res4;
|
||||
uint16_t iomap_offset;
|
||||
};
|
||||
#endif
|
||||
typedef struct tss tss_t;
|
||||
|
||||
extern tss_t tss;
|
||||
|
||||
void set_ltr(uint16_t seg);
|
||||
|
||||
#endif //_KERNEL_PLATFORM_X86_TASKING_TSS_H
|
||||
5
kernel/include/platform/x86/vmm/consts.h
Normal file
5
kernel/include/platform/x86/vmm/consts.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#ifdef __i386__
|
||||
#include <platform/x86/i386/vmm/consts.h>
|
||||
#elif __x86_64__
|
||||
#include <platform/x86/x86_64/vmm/consts.h>
|
||||
#endif
|
||||
5
kernel/include/platform/x86/vmm/mmu.h
Normal file
5
kernel/include/platform/x86/vmm/mmu.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#ifdef __i386__
|
||||
#include <platform/x86/i386/vmm/mmu.h>
|
||||
#elif __x86_64__
|
||||
#include <platform/x86/x86_64/vmm/mmu.h>
|
||||
#endif
|
||||
30
kernel/include/platform/x86/x86_64/tasking/context.h
Normal file
30
kernel/include/platform/x86/x86_64/tasking/context.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_X86_64_TASKING_CONTEXT_H
|
||||
#define _KERNEL_PLATFORM_X86_X86_64_TASKING_CONTEXT_H
|
||||
|
||||
#include <libkern/c_attrs.h>
|
||||
#include <libkern/types.h>
|
||||
|
||||
struct PACKED context {
|
||||
uint64_t r15;
|
||||
uint64_t r14;
|
||||
uint64_t r13;
|
||||
uint64_t r12;
|
||||
uint64_t r11;
|
||||
uint64_t r10;
|
||||
uint64_t rbx;
|
||||
uint64_t rbp;
|
||||
uint64_t rip;
|
||||
};
|
||||
typedef struct context context_t;
|
||||
|
||||
static inline uintptr_t context_get_instruction_pointer(context_t* ctx)
|
||||
{
|
||||
return ctx->rip;
|
||||
}
|
||||
|
||||
static inline void context_set_instruction_pointer(context_t* ctx, uintptr_t ip)
|
||||
{
|
||||
ctx->rip = ip;
|
||||
}
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_X86_64_TASKING_CONTEXT_H
|
||||
120
kernel/include/platform/x86/x86_64/tasking/trapframe.h
Normal file
120
kernel/include/platform/x86/x86_64/tasking/trapframe.h
Normal file
@@ -0,0 +1,120 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_X86_64_TASKING_TRAPFRAME_H
|
||||
#define _KERNEL_PLATFORM_X86_X86_64_TASKING_TRAPFRAME_H
|
||||
|
||||
#include <libkern/c_attrs.h>
|
||||
#include <libkern/types.h>
|
||||
#include <platform/x86/gdt.h>
|
||||
#include <platform/x86/tasking/tss.h>
|
||||
|
||||
struct PACKED trapframe {
|
||||
// all registers
|
||||
uint64_t r15;
|
||||
uint64_t r14;
|
||||
uint64_t r13;
|
||||
uint64_t r12;
|
||||
uint64_t r11;
|
||||
uint64_t r10;
|
||||
uint64_t r9;
|
||||
uint64_t r8;
|
||||
uint64_t rbp;
|
||||
uint64_t rdi;
|
||||
uint64_t rsi;
|
||||
uint64_t rdx;
|
||||
uint64_t rcx;
|
||||
uint64_t rbx;
|
||||
uint64_t rax;
|
||||
|
||||
// rest of trap frame
|
||||
uint64_t gs;
|
||||
uint64_t fs;
|
||||
uint64_t int_no;
|
||||
|
||||
// below here defined by x86 hardware
|
||||
uint64_t err;
|
||||
uint64_t rip;
|
||||
uint64_t cs;
|
||||
|
||||
// below here only when crossing rings, such as from user to kernel
|
||||
uint64_t rflags;
|
||||
uint64_t rsp;
|
||||
uint64_t ss;
|
||||
};
|
||||
typedef struct trapframe trapframe_t;
|
||||
|
||||
static inline uintptr_t get_stack_pointer(trapframe_t* tf)
|
||||
{
|
||||
return tf->rsp;
|
||||
}
|
||||
|
||||
static inline void set_stack_pointer(trapframe_t* tf, uintptr_t sp)
|
||||
{
|
||||
tf->rsp = sp;
|
||||
}
|
||||
|
||||
static inline uintptr_t get_frame_pointer(trapframe_t* tf)
|
||||
{
|
||||
return tf->rbp;
|
||||
}
|
||||
|
||||
static inline void set_frame_pointer(trapframe_t* tf, uintptr_t bp)
|
||||
{
|
||||
tf->rbp = bp;
|
||||
}
|
||||
|
||||
static inline uintptr_t get_instruction_pointer(trapframe_t* tf)
|
||||
{
|
||||
return tf->rip;
|
||||
}
|
||||
|
||||
static inline void set_instruction_pointer(trapframe_t* tf, uintptr_t ip)
|
||||
{
|
||||
tf->rip = ip;
|
||||
}
|
||||
|
||||
static inline uintptr_t get_syscall_result(trapframe_t* tf)
|
||||
{
|
||||
return tf->rax;
|
||||
}
|
||||
|
||||
static inline void set_syscall_result(trapframe_t* tf, uintptr_t val)
|
||||
{
|
||||
tf->rax = val;
|
||||
}
|
||||
|
||||
/**
|
||||
* STACK FUNCTIONS
|
||||
*/
|
||||
|
||||
static inline void tf_push_to_stack(trapframe_t* tf, uintptr_t val)
|
||||
{
|
||||
tf->rsp -= sizeof(uintptr_t);
|
||||
*((uintptr_t*)tf->rsp) = val;
|
||||
}
|
||||
|
||||
static inline uintptr_t tf_pop_to_stack(trapframe_t* tf)
|
||||
{
|
||||
uintptr_t val = *((uintptr_t*)tf->rsp);
|
||||
tf->rsp += sizeof(uintptr_t);
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline void tf_move_stack_pointer(trapframe_t* tf, int32_t val)
|
||||
{
|
||||
tf->rsp += val;
|
||||
}
|
||||
|
||||
static inline void tf_setup_as_user_thread(trapframe_t* tf)
|
||||
{
|
||||
tf->cs = (GDT_SEG_UCODE << 3) | DPL_USER;
|
||||
tf->ss = (GDT_SEG_UDATA << 3) | DPL_USER;
|
||||
tf->rflags = FL_IF;
|
||||
}
|
||||
|
||||
static inline void tf_setup_as_kernel_thread(trapframe_t* tf)
|
||||
{
|
||||
tf->cs = (GDT_SEG_KCODE << 3);
|
||||
tf->ss = (GDT_SEG_KDATA << 3);
|
||||
tf->rflags = FL_IF;
|
||||
}
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_X86_64_TASKING_TRAPFRAME_H
|
||||
35
kernel/include/platform/x86/x86_64/vmm/consts.h
Normal file
35
kernel/include/platform/x86/x86_64/vmm/consts.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_X86_64_VMM_CONSTS_H
|
||||
#define _KERNEL_PLATFORM_X86_X86_64_VMM_CONSTS_H
|
||||
|
||||
static inline int vm_page_size()
|
||||
{
|
||||
return 0x1000;
|
||||
}
|
||||
|
||||
static inline int vm_page_mask()
|
||||
{
|
||||
return 0xfff;
|
||||
}
|
||||
|
||||
#define VMM_LV0_ENTITY_COUNT (512)
|
||||
#define VMM_LV1_ENTITY_COUNT (512)
|
||||
#define VMM_LV2_ENTITY_COUNT (512)
|
||||
#define VMM_LV3_ENTITY_COUNT (512)
|
||||
#define VMM_PAGE_SIZE (vm_page_size())
|
||||
|
||||
#define PAGE_START(vaddr) ((vaddr & (~(uintptr_t)vm_page_mask())))
|
||||
#define FRAME(addr) (addr / VMM_PAGE_SIZE)
|
||||
|
||||
#define PTABLE_LV_TOP (3)
|
||||
#define PTABLE_LV0_VADDR_OFFSET (12)
|
||||
#define PTABLE_LV1_VADDR_OFFSET (21)
|
||||
#define PTABLE_LV2_VADDR_OFFSET (30)
|
||||
#define PTABLE_LV3_VADDR_OFFSET (39)
|
||||
|
||||
#define PTABLE_TOP_KERNEL_OFFSET (VMM_LV3_ENTITY_COUNT / 2)
|
||||
|
||||
#define USER_HIGH 0x7fffffffffff
|
||||
#define KERNEL_BASE 0xffff800000000000
|
||||
#define KERNEL_PADDR_BASE 0xffffffff00000000 // TODO(x64): up to 4gbs are supported.
|
||||
|
||||
#endif //_KERNEL_PLATFORM_X86_X86_64_VMM_CONSTS_H
|
||||
30
kernel/include/platform/x86/x86_64/vmm/mmu.h
Normal file
30
kernel/include/platform/x86/x86_64/vmm/mmu.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef _KERNEL_PLATFORM_X86_X86_64_VMM_MMU_H
|
||||
#define _KERNEL_PLATFORM_X86_X86_64_VMM_MMU_H
|
||||
|
||||
#include <libkern/types.h>
|
||||
#include <mem/bits/mmu.h>
|
||||
|
||||
// x86_64 uses one table to manage virtual space.
|
||||
#undef DOUBLE_TABLE_PAGING
|
||||
|
||||
typedef uint64_t ptable_entity_t;
|
||||
typedef uint64_t arch_pf_info_t;
|
||||
|
||||
ptable_entity_t vm_mmu_to_arch_flags(mmu_flags_t mmu_flags, ptable_lv_t lv);
|
||||
mmu_flags_t vm_arch_to_mmu_flags(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
mmu_pf_info_flags_t vm_arch_parse_pf_info(arch_pf_info_t info);
|
||||
|
||||
void vm_ptable_entity_set_default_flags(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
void vm_ptable_entity_allocated(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
void vm_ptable_entity_invalidate(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
|
||||
void vm_ptable_entity_set_mmu_flags(ptable_entity_t* entity, ptable_lv_t lv, mmu_flags_t mmu_flags);
|
||||
void vm_ptable_entity_rm_mmu_flags(ptable_entity_t* entity, ptable_lv_t lv, mmu_flags_t mmu_flags);
|
||||
void vm_ptable_entity_set_frame(ptable_entity_t* entity, ptable_lv_t lv, uintptr_t frame);
|
||||
uintptr_t vm_ptable_entity_get_frame(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
|
||||
ptable_state_t vm_ptable_entity_state(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
bool vm_ptable_entity_is_present(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
bool vm_ptable_entity_is_only_allocated(ptable_entity_t* entity, ptable_lv_t lv);
|
||||
|
||||
#endif // _KERNEL_PLATFORM_X86_X86_64_VMM_MMU_H
|
||||
Reference in New Issue
Block a user