summaryrefslogtreecommitdiffstats
path: root/include/arch/x86_64
diff options
context:
space:
mode:
Diffstat (limited to 'include/arch/x86_64')
-rw-r--r--include/arch/x86_64/cpu.h6
-rw-r--r--include/arch/x86_64/elf.h93
-rw-r--r--include/arch/x86_64/gdt.h23
-rw-r--r--include/arch/x86_64/idt.h25
-rw-r--r--include/arch/x86_64/msr.h56
-rw-r--r--include/arch/x86_64/page.h40
-rw-r--r--include/arch/x86_64/processor.h17
-rw-r--r--include/arch/x86_64/tables.h57
-rw-r--r--include/arch/x86_64/tss.h20
9 files changed, 337 insertions, 0 deletions
diff --git a/include/arch/x86_64/cpu.h b/include/arch/x86_64/cpu.h
new file mode 100644
index 0000000..6e467c1
--- /dev/null
+++ b/include/arch/x86_64/cpu.h
@@ -0,0 +1,6 @@
+#ifndef _JOVE_x86_64_CPU_H
+#define _JOVE_x86_64_CPU_H 1
+
+void cpu_arch_enable_sce(void);
+
+#endif
diff --git a/include/arch/x86_64/elf.h b/include/arch/x86_64/elf.h
new file mode 100644
index 0000000..1aaf116
--- /dev/null
+++ b/include/arch/x86_64/elf.h
@@ -0,0 +1,93 @@
+#ifndef _JOVE_x86_64_ELF_H
+#define _JOVE_x86_64_ELF_H 1
+
+#include <stdint.h>
+
+#define EI_MAG0 0x7F
+#define EI_MAG1 'E'
+#define EI_MAG2 'L'
+#define EI_MAG3 'F'
+
+#define EI_CLASS_32 1
+#define EI_CLASS_64 2
+
+#define EI_DATA_LE 1
+#define EI_DATA_BE 2
+
+#define EI_VERSION_CURRENT 1
+
+#define ET_NONE 0
+#define ET_REL 1
+#define ET_EXEC 2
+#define ET_DYN 3
+
+typedef struct elf64_file_header
+{
+ union {
+ struct {
+ uint8_t ei_mag[4];
+ uint8_t ei_class;
+ uint8_t ei_data;
+ uint8_t ei_version;
+ uint8_t ei_osabi;
+ uint8_t ei_abiver;
+ uint8_t ei_pad[7];
+ };
+ uint8_t e_ident[16];
+ };
+ uint16_t e_type;
+ uint16_t e_machine;
+ uint32_t e_version;
+ uint64_t e_entry;
+ uint64_t e_phoff;
+ uint64_t e_shoff;
+ uint32_t e_flags;
+ uint16_t e_ehsize;
+ uint16_t e_phentsize;
+ uint16_t e_phnum;
+ uint16_t e_shentsize;
+ uint16_t e_shnum;
+ uint16_t e_shstrndx;
+} elf_header_t;
+
+#define PT_NULL 0
+#define PT_LOAD 1
+#define PT_DYNAMIC 2
+#define PT_INTERP 3
+#define PT_NOTE 4
+#define PT_SHLIB 5
+#define PT_PHDR 6
+#define PT_TLS 7
+
+#define PF_X 1
+#define PF_W 2
+#define PF_R 4
+
+typedef struct elf64_program_header
+{
+ uint32_t p_type;
+ uint32_t p_flags;
+ uint64_t p_offset;
+ uint64_t p_vaddr;
+ uint64_t p_paddr;
+ uint64_t p_filesz;
+ uint64_t p_memsz;
+ uint64_t p_align;
+} elf_phdr_t;
+
+#define SHT_NULL 0
+#define SHT_PROGBITS 1
+
+typedef struct elf64_section_header
+{
+ uint32_t sh_name;
+ uint32_t sh_type;
+ uint64_t sh_flags;
+ uint64_t sh_addr;
+ uint64_t sh_offset;
+ uint64_t sh_size;
+ uint64_t sh_addralign;
+ uint64_t sh_entsize;
+} elf_shdr_t;
+
+#endif
diff --git a/include/arch/x86_64/gdt.h b/include/arch/x86_64/gdt.h
new file mode 100644
index 0000000..c17c9eb
--- /dev/null
+++ b/include/arch/x86_64/gdt.h
@@ -0,0 +1,23 @@
+#ifndef _JOVE_x86_64_GDT_H
+#define _JOVE_x86_64_GDT_H 1
+
+#include "tables.h"
+
+enum
+{
+ GDT_SEGMENT_KERNEL_NULL = 0,
+ GDT_SEGMENT_KERNEL_CODE,
+ GDT_SEGMENT_KERNEL_DATA,
+
+ GDT_SEGMENT_USER_NULL,
+ GDT_SEGMENT_USER_DATA,
+ GDT_SEGMENT_USER_CODE,
+
+ GDT_SEGMENT_TSS_LOW,
+ GDT_SEGMENT_TSS_HIGH,
+
+ GDT_SEGMENT_COUNT
+};
+typedef segment_descriptor_t gdt_t[GDT_SEGMENT_COUNT];
+
+#endif
diff --git a/include/arch/x86_64/idt.h b/include/arch/x86_64/idt.h
new file mode 100644
index 0000000..7a5c468
--- /dev/null
+++ b/include/arch/x86_64/idt.h
@@ -0,0 +1,25 @@
+#ifndef _JOVE_x86_64_IDT_H
+#define _JOVE_x86_64_IDT_H 1
+
+#include "tables.h"
+
+typedef struct interrupt_state
+{
+ uint64_t r15, r14, r13, r12, r11, r10, r9, r8;
+ uint64_t bp, di, si, dx, cx, bx, ax;
+ uint64_t ip, cs, flags, sp, ss;
+} int_state_t;
+
+typedef interrupt_gate_t idt_t[48];
+
+typedef int_state_t *(*int_handler_t)(int_state_t *);
+
+void int_handler_set(uint8_t i, int_handler_t handler);
+int_handler_t int_handler_get(uint8_t i);
+
+void int_state_print(int_state_t *state);
+
+extern uint64_t __isr_err;
+extern uint64_t __isr_num;
+
+#endif
diff --git a/include/arch/x86_64/msr.h b/include/arch/x86_64/msr.h
new file mode 100644
index 0000000..c20f8fc
--- /dev/null
+++ b/include/arch/x86_64/msr.h
@@ -0,0 +1,56 @@
+#ifndef _JOVE_x86_64_MSR_H
+#define _JOVE_x86_64_MSR_H 1
+
+#include <stddef.h>
+#include <stdint.h>
+
+#define MSR_EFER 0xC0000080
+#define MSR_STAR 0xC0000081
+#define MSR_LSTAR 0xC0000082
+#define MSR_SFMASK 0xC0000084
+
+#define MSR_FSBASE 0xC0000100
+#define MSR_GSBASE 0xC0000101
+#define MSR_KERNELGSBASE 0xC0000102
+
+typedef union msr_efer
+{
+ struct {
+ uint8_t sce : 1;
+ uint8_t resv : 7;
+ uint8_t lme : 1;
+ uint8_t unk0 : 1;
+ uint8_t lma : 1;
+ uint8_t nxe : 1;
+ uint8_t svme : 1;
+ uint8_t lmsle : 1;
+ uint8_t ffxsr : 1;
+ uint8_t tce : 1;
+ };
+ uint32_t v[2];
+} msr_efer_t;
+
+typedef union msr_star
+{
+ struct {
+ uint32_t eip;
+ uint16_t kcs;
+ uint16_t ucs;
+ };
+ uint32_t v[2];
+} msr_star_t;
+typedef uintptr_t msr_lstar_t;
+
+void msr_efer_write(msr_efer_t v);
+msr_efer_t msr_efer_read(void);
+
+void msr_star_write(msr_star_t v);
+msr_star_t msr_star_read(void);
+
+void msr_lstar_write(msr_lstar_t v);
+msr_lstar_t msr_lstar_read(void);
+
+void msr_gsbase_write(uintptr_t gsbase);
+uintptr_t msr_gsbase_read(void);
+
+#endif
diff --git a/include/arch/x86_64/page.h b/include/arch/x86_64/page.h
new file mode 100644
index 0000000..cbbf642
--- /dev/null
+++ b/include/arch/x86_64/page.h
@@ -0,0 +1,40 @@
+#ifndef _JOVE_x86_64_PAGE_H
+#define _JOVE_x86_64_PAGE_H 1
+
+#include <stdint.h>
+#include "klib/spinlock.h"
+#include "assert.h"
+
+typedef union page_mapping_level_entry
+{
+ 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 struct page_directory
+{
+ spinlock_t lock;
+ intmax_t id;
+ uintptr_t phys;
+ pmle_t *pml;
+} page_directory_t;
+
+#define _kernel_virtual_base 0xFFFFFFFF80000000ULL
+
+#endif
diff --git a/include/arch/x86_64/processor.h b/include/arch/x86_64/processor.h
new file mode 100644
index 0000000..c208eb1
--- /dev/null
+++ b/include/arch/x86_64/processor.h
@@ -0,0 +1,17 @@
+#ifndef _JOVE_x86_64_PROCESSOR_H
+#define _JOVE_x86_64_PROCESSOR_H 1
+
+#include "tables.h"
+#include "gdt.h"
+#include "idt.h"
+#include "tss.h"
+
+#define PROCESSOR_MAX 8
+
+#define ARCH_SPECIFIC_PROCESSOR_MEMBERS \
+ __attribute__((aligned(0x1000))) gdt_t _gdt; \
+ struct TSS _tss; \
+ struct XDTR _gdtr; \
+ struct XDTR _idtr
+
+#endif
diff --git a/include/arch/x86_64/tables.h b/include/arch/x86_64/tables.h
new file mode 100644
index 0000000..e311192
--- /dev/null
+++ b/include/arch/x86_64/tables.h
@@ -0,0 +1,57 @@
+#ifndef _JOVE_X86_64_TABLES_H
+#define _JOVE_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 segment_descriptor
+{
+ 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 interrupt_gate
+{
+ 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;
+
+struct XDTR /* Generic table descriptor struct */
+{
+ uint16_t length;
+ uint64_t address;
+}__attribute__((packed));
+
+#endif
diff --git a/include/arch/x86_64/tss.h b/include/arch/x86_64/tss.h
new file mode 100644
index 0000000..3184df5
--- /dev/null
+++ b/include/arch/x86_64/tss.h
@@ -0,0 +1,20 @@
+#ifndef JOVE_ARCH_x86_64_TSS_H
+#define JOVE_ARCH_x86_64_TSS_H 1
+
+#include <stddef.h>
+#include <stdint.h>
+
+struct TSS
+{
+ uint32_t resv0;
+ uint32_t rsp[3][2];
+ uint64_t resv1;
+ uint32_t ist[8][2];
+ uint64_t resv2;
+ uint16_t resv3;
+ uint16_t iobp;
+};
+
+void tss_set_rsp(struct TSS *tss, uint8_t dpl, uintptr_t rsp);
+
+#endif