summaryrefslogtreecommitdiffstats
path: root/include/arch
diff options
context:
space:
mode:
authorJon Santmyer <jon@jonsantmyer.com>2024-05-22 13:00:41 -0400
committerJon Santmyer <jon@jonsantmyer.com>2024-05-22 13:00:41 -0400
commitace65b453151845bc361f21f3e5b651c35f9f126 (patch)
tree262ebd29b0ca1d8584f0b6f1efa7a00d9f4f3e43 /include/arch
parentf004c1ade8d617a82cea2fe249434cccb47a2358 (diff)
downloadjove-kernel-master.tar.gz
jove-kernel-master.tar.bz2
jove-kernel-master.zip
massive refactor for mp and organizationHEADmaster
Diffstat (limited to 'include/arch')
-rw-r--r--include/arch/cpu.h14
-rw-r--r--include/arch/elf.h12
-rw-r--r--include/arch/page.h34
-rw-r--r--include/arch/processor.h39
-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
13 files changed, 436 insertions, 0 deletions
diff --git a/include/arch/cpu.h b/include/arch/cpu.h
new file mode 100644
index 0000000..fe89dc0
--- /dev/null
+++ b/include/arch/cpu.h
@@ -0,0 +1,14 @@
+#ifndef _JOVE_ARCH_CPU_H
+#define _JOVE_ARCH_CPU_H 1
+
+#ifdef __x86_64__
+#include "x86_64/cpu.h"
+#endif
+
+void cpu_setup(void);
+
+void cpu_set_syscall_entry(void *entry);
+
+void umode_enter(void *ip, void *sp);
+
+#endif
diff --git a/include/arch/elf.h b/include/arch/elf.h
new file mode 100644
index 0000000..1b8c3dc
--- /dev/null
+++ b/include/arch/elf.h
@@ -0,0 +1,12 @@
+#ifndef _JOVE_ARCH_ELF_H
+#define _JOVE_ARCH_ELF_H 1
+
+#ifdef __x86_64__
+#include "x86_64/elf.h"
+#endif
+
+#include <stddef.h>
+
+uintptr_t elf_load(const void *data, size_t len);
+
+#endif
diff --git a/include/arch/page.h b/include/arch/page.h
new file mode 100644
index 0000000..25c5294
--- /dev/null
+++ b/include/arch/page.h
@@ -0,0 +1,34 @@
+#ifndef _JOVE_ARCH_PAGE_H
+#define _JOVE_ARCH_PAGE_H 1
+
+#define PAGE_SHIFT (12)
+#define PAGE_SIZE (1 << PAGE_SHIFT)
+#define PAGE_MASK (PAGE_SIZE - 1)
+
+#ifdef __x86_64__
+#include "x86_64/page.h"
+#endif
+
+typedef struct page_mapping_flags
+{
+ char present : 1;
+ char writeable : 1;
+ char useraccess : 1;
+ char executable : 1;
+} page_flags_t;
+
+typedef struct page_mapping
+{
+ uintptr_t phys;
+ page_flags_t pf;
+} page_mapping_t;
+
+typedef intmax_t pdid_t;
+
+page_directory_t *pd_new(void);
+page_directory_t *pd_dup(page_directory_t *pd);
+page_directory_t *pd_get(pdid_t id);
+
+void pd_switch(page_directory_t *pd);
+
+#endif
diff --git a/include/arch/processor.h b/include/arch/processor.h
new file mode 100644
index 0000000..871e5c7
--- /dev/null
+++ b/include/arch/processor.h
@@ -0,0 +1,39 @@
+#ifndef _JOVE_ARCH_PROCESSOR_H
+#define _JOVE_ARCH_PROCESSOR_H 1
+
+#ifdef __x86_64__
+#include "x86_64/processor.h"
+#endif
+
+#include "memory.h"
+#include "tasking.h"
+#include "klib/spinlock.h"
+
+#define _EXRTAB_C (PAGE_SIZE / sizeof(uintptr_t))
+
+typedef struct processor
+{
+ ARCH_SPECIFIC_PROCESSOR_MEMBERS;
+ unsigned int id;
+ unsigned int isr_depth;
+ page_directory_t *pd;
+ tcb_t *tcb;
+
+ uintptr_t exrtab[_EXRTAB_C];
+ size_t ert_i;
+} processor_t;
+
+extern processor_t *proc_bsp;
+
+void processor_setup(processor_t *proc);
+
+processor_t *processor_list(void);
+
+processor_t *processor_current(void);
+page_directory_t *pd_current(void);
+tcb_t *tcb_current(void);
+
+void _exrtab_push(void *v);
+void *_exrtab_pop(void);
+
+#endif
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