diff options
Diffstat (limited to 'include/arch/x86_64')
-rw-r--r-- | include/arch/x86_64/idt.h | 18 | ||||
-rw-r--r-- | include/arch/x86_64/object.h | 26 | ||||
-rw-r--r-- | include/arch/x86_64/page.h | 41 | ||||
-rw-r--r-- | include/arch/x86_64/processor.h | 66 | ||||
-rw-r--r-- | include/arch/x86_64/tables.h | 51 |
5 files changed, 202 insertions, 0 deletions
diff --git a/include/arch/x86_64/idt.h b/include/arch/x86_64/idt.h new file mode 100644 index 0000000..90ce48b --- /dev/null +++ b/include/arch/x86_64/idt.h @@ -0,0 +1,18 @@ +#ifndef _JOVE_x86_64_IDT_H +#define _JOVE_x86_64_IDT_H 1 + +#include "processor.h" + +typedef struct jove_IVTState +{ + uint64_t r15, r14, r13, r12, r11, r10, r9, r8; + uint64_t rbp, rdi, rsi, rdx, rcx, rbx, rax; + uint64_t rip, cs, rflags, rsp, ss; +} ivt_state_t; + +void kpanic_state(ivt_state_t *state, const char *fmt, ...); + +void ivt_setup(void); +void idt_setup(processor_t *processor); + +#endif diff --git a/include/arch/x86_64/object.h b/include/arch/x86_64/object.h new file mode 100644 index 0000000..8d5af33 --- /dev/null +++ b/include/arch/x86_64/object.h @@ -0,0 +1,26 @@ +#ifndef _JOVE_x86_64_OBJECT_H +#define _JOVE_x86_64_OBJECT_H 1 + +#include <stdint.h> + +typedef struct jove_InitData +{ + uint8_t log_object; + uint8_t untyped_data_dir; + uint8_t processor_dir; + uint8_t pm_object; //Page mapping object. + uint8_t initrd_dir; //Init ramdisk files directory. + uint8_t tcb_object; + uint8_t kernel_stack_object; + uint8_t message_object; + uintptr_t message_object_address; +} init_data_t; + +typedef struct jove_ThreadControlBlock +{ + void *stack; + uintptr_t sp, ksp; + void *pml4; +} tcb_t; + +#endif diff --git a/include/arch/x86_64/page.h b/include/arch/x86_64/page.h new file mode 100644 index 0000000..99bc691 --- /dev/null +++ b/include/arch/x86_64/page.h @@ -0,0 +1,41 @@ +#ifndef _JOVE_ARCH_x86_64_PAGE_H +#define _JOVE_ARCH_x86_64_PAGE_H 1 + +#include <stdint.h> +#include "include/object.h" + +typedef union jove_PageMapLevelEntry +{ + struct { + uint8_t p : 1; /* Present */ + uint8_t rw : 1; /* Read/write. 0 for RO.*/ + uint8_t us : 1; /* User/supervisor. 0 for DPL3 forbid */ + uint8_t pwt : 1; + uint8_t pcd : 1; + uint8_t a : 1; /* Accessed */ + uint8_t d : 1; /* Dirty */ + uint8_t ps_pat : 1; + uint8_t g : 1; /* Global */ + uint8_t _r0 : 2; + uint8_t r : 1; + uint64_t paddr : 35; + uint8_t _r1; + uint8_t pk : 4; + uint8_t xd : 1; + }__attribute__((packed)); + uint64_t value; +} __attribute__((packed)) pmle_t; + +typedef uint16_t pmli_t; + +#define PML_SHL(l) ((l * 9) + 3) +#define PML_I_FOR_LAYER(v, l) ((v >> PML_SHL(l)) % 512) + +uintptr_t vmem_ident_tophys(void *vptr); +void *vmem_phys_tovirt(uintptr_t pptr); + +void *pmle_get_page(pmle_t entry); + +int untyped_retype_page(objdir_entry_t *untyped_entry, void **dest_ptr); + +#endif diff --git a/include/arch/x86_64/processor.h b/include/arch/x86_64/processor.h new file mode 100644 index 0000000..f8a93ce --- /dev/null +++ b/include/arch/x86_64/processor.h @@ -0,0 +1,66 @@ +#ifndef _JOVE_ARCH_x86_64_PROCESSOR_H +#define _JOVE_ARCH_x86_64_PROCESSOR_H 1 + +#include "memory.h" +#include "tables.h" +#include "object.h" +#include <stdint.h> + +#define MSR_FS_BASE 0xC0000100 +#define MSR_GS_BASE 0xC0000101 +#define MSR_KGS_BASE 0xC0000102 + +#define MSR_EFER 0xC0000080 +#define MSR_STAR 0xC0000081 +#define MSR_LSTAR 0xC0000082 +#define MSR_SFMASK 0xC0000084 + +typedef struct jove_TSS +{ + uint32_t resv0; + uint64_t rsp[3]; + uint32_t resv1; + uint64_t ist[8]; + uint32_t resv2[2]; + uint16_t resv3; + uint16_t iopb; +} tss_t; + +enum +{ + GDT_ENTRY_KERNEL_NULL = 0, + GDT_ENTRY_KERNEL_CODE, + GDT_ENTRY_KERNEL_DATA, + GDT_ENTRY_USER_NULL, + GDT_ENTRY_USER_DATA, + GDT_ENTRY_USER_CODE, + GDT_ENTRY_TSS_LOW, + GDT_ENTRY_TSS_HIGH, + GDT_ENTRY_COUNT +}; + +typedef struct jove_Processor +{ + physptr_t pdir; + struct jove_ObjectDirectory *odir; + + segment_descriptor_t gdt[GDT_ENTRY_COUNT]; + struct { + uint16_t length; + uint64_t base; + } __attribute__((packed)) gdtr; + struct { + uint16_t length; + uint64_t base; + } __attribute__((packed)) idtr; + + tss_t tss; + tcb_t *tcb; +} processor_t; + +void gdt_setup(processor_t *processor); + +void rdmsr(uint32_t msr, uint32_t *lo, uint32_t *hi); +void wrmsr(uint32_t msr, uint32_t lo, uint32_t hi); + +#endif diff --git a/include/arch/x86_64/tables.h b/include/arch/x86_64/tables.h new file mode 100644 index 0000000..42651a1 --- /dev/null +++ b/include/arch/x86_64/tables.h @@ -0,0 +1,51 @@ +#ifndef _JOVE_ARCH_x86_64_TABLES_H +#define _JOVE_ARCH_x86_64_TABLES_H 1 + +#include <stdint.h> + +#define CD_SEGMENT_TYPE_ACCESSED 1 +#define CD_SEGMENT_TYPE_WRITEABLE 2 +#define CD_SEGMENT_TYPE_DATA_EXPAND_DOWN 4 +#define CD_SEGMENT_TYPE_CODE_CONFORMING 4 +#define CD_SEGMENT_TYPE_CODE 8 + +#define S_SEGMENT_TYPE_LDT 2 +#define S_SEGMENT_TYPE_TSS_AVAIL 9 +#define S_SEGMENT_TYPE_TSS_BUSY 11 +#define S_SEGMENT_TYPE_CALLGATE 12 +#define S_SEGMENT_TYPE_INT_GATE 14 +#define S_SEGMENT_TYPE_TRAP_GATE 15 + +typedef struct jove_SegmentDescriptor +{ + uint16_t limit_0_15; /* Segment limit. */ + uint16_t base_0_15; /* Segment base. */ + uint8_t base_16_23; + uint8_t type : 4; /* Segment type. */ + uint8_t s : 1; /* Descriptor type (0 = system, 1 = code/data)*/ + uint8_t dpl : 2; /* Descriptor privilege level. */ + uint8_t p : 1; /* Present. */ + uint8_t limit_16_19 : 4; + uint8_t avl : 1; /* Available for use by system software. */ + uint8_t l : 1; /* 64-bit segment (Ext). */ + uint8_t d_b : 1; /* Default operation size (0 = 16-bit, 1 = 32-bit)*/ + uint8_t g : 1; /* Granularity. */ + uint8_t base_24_31; +}__attribute__((packed)) segment_descriptor_t; + +typedef struct jove_InterruptGate +{ + uint16_t base_0_15; + uint16_t segment_selector; + uint8_t ist : 3; + uint8_t zero_0 : 5; + uint8_t type : 4; + uint8_t zero_1 : 1; + uint8_t dpl : 2; + uint8_t p : 1; + uint16_t base_16_31; + uint32_t base_32_63; + uint32_t resv; +}__attribute__((packed)) interrupt_gate_t; + +#endif |