summaryrefslogtreecommitdiff
path: root/sys/include/arch
diff options
context:
space:
mode:
Diffstat (limited to 'sys/include/arch')
-rw-r--r--sys/include/arch/amd64/frame.h116
-rw-r--r--sys/include/arch/amd64/gdt.h80
-rw-r--r--sys/include/arch/amd64/idt.h72
-rw-r--r--sys/include/arch/amd64/trap.h78
4 files changed, 346 insertions, 0 deletions
diff --git a/sys/include/arch/amd64/frame.h b/sys/include/arch/amd64/frame.h
new file mode 100644
index 0000000..6239eff
--- /dev/null
+++ b/sys/include/arch/amd64/frame.h
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2023 Ian Marco Moffett and the VegaOS team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of VegaOS nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* $Id$ */
+
+#ifndef _AMD64_FRAME_H_
+#define _AMD64_FRAME_H_
+
+#if !defined(__ASSEMBLER__)
+struct trapframe {
+ uint64_t trapno;
+ uint64_t rax;
+ uint64_t rcx;
+ uint64_t rdx;
+ uint64_t rbx;
+ uint64_t rsi;
+ uint64_t rdi;
+ uint64_t rbp;
+ uint64_t r8;
+ uint64_t r9;
+ uint64_t r10;
+ uint64_t r11;
+ uint64_t r12;
+ uint64_t r13;
+ uint64_t r14;
+ uint64_t r15;
+ /* Pushed by hardware */
+ uint64_t error_code;
+ uint64_t rip;
+ uint64_t cs;
+ uint64_t rflags;
+ uint64_t rsp;
+ uint64_t ss;
+};
+#else
+/*
+ * XXX: Before this macro is invoked,
+ * you should determine if an error
+ * code will be present already on the
+ * stack. If not, push a null qword as
+ * padding (e.g push $0).
+ *
+ * There *must* be a value used
+ * as an error code whether that be
+ * a real error code or just padding.
+ *
+ * Failing to do so will result in
+ * undefined behaviour.
+ *
+ */
+.macro push_trapframe trapno
+ push %r15
+ push %r14
+ push %r13
+ push %r12
+ push %r11
+ push %r10
+ push %r9
+ push %r8
+ push %rbp
+ push %rdi
+ push %rsi
+ push %rbx
+ push %rdx
+ push %rcx
+ push %rax
+ push \trapno
+.endm
+
+.macro pop_trapframe trapno
+ add $8, %rsp /* Trapno */
+ pop %rax
+ pop %rcx
+ pop %rdx
+ pop %rbx
+ pop %rsi
+ pop %rdi
+ pop %rbp
+ pop %r8
+ pop %r9
+ pop %r10
+ pop %r12
+ pop %r13
+ pop %r14
+ pop %r15
+ add $8, %rsp /* Pop error code */
+ iretq
+.endm
+#endif /* !defined(__ASSEMBLER__) */
+#endif /* !_AMD64_FRAME_H_ */
diff --git a/sys/include/arch/amd64/gdt.h b/sys/include/arch/amd64/gdt.h
new file mode 100644
index 0000000..756e2a6
--- /dev/null
+++ b/sys/include/arch/amd64/gdt.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2023 Ian Marco Moffett and the VegaOS team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of VegaOS nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* $Id$ */
+
+#ifndef AMD64_GDT_H_
+#define AMD64_GDT_H_
+
+#include <sys/types.h>
+#include <sys/cdefs.h>
+
+#define GDT_TSS 5
+#define KERNEL_CS 0x8
+#define KERNEL_DS 0x10
+
+struct __packed gdt_entry {
+ uint16_t limit;
+ uint16_t base_low;
+ uint8_t base_mid;
+ uint8_t access;
+ uint8_t granularity;
+ uint8_t base_hi;
+};
+
+struct __packed gdtr {
+ uint16_t limit;
+ uintptr_t offset;
+};
+
+static inline void
+gdt_load(struct gdtr *gdtr)
+{
+ __asm("lgdt %0\n"
+ "push $8\n" /* Push CS */
+ "lea 1f(%%rip), %%rax\n" /* Load 1 label address into RAX */
+ "push %%rax\n" /* Push the return address (label 1) */
+ "lretq\n" /* Far return to update CS */
+ "1:\n"
+ " mov $0x10, %%eax\n"
+ " mov %%eax, %%ds\n"
+ " mov %%eax, %%es\n"
+ " mov %%eax, %%fs\n"
+ " mov %%eax, %%gs\n"
+ " mov %%eax, %%ss\n"
+ :
+ : "m" (*gdtr)
+ : "rax", "memory"
+ );
+}
+
+extern struct gdt_entry g_dmmy_gdt[256];
+extern struct gdtr g_early_gdtr;
+
+#endif /* !AMD64_GDT_H_ */
diff --git a/sys/include/arch/amd64/idt.h b/sys/include/arch/amd64/idt.h
new file mode 100644
index 0000000..d3b623d
--- /dev/null
+++ b/sys/include/arch/amd64/idt.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2023 Ian Marco Moffett and the VegaOS team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of VegaOS nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* $Id$ */
+
+#ifndef _AMD64_IDT_H_
+#define _AMD64_IDT_H_
+
+#include <sys/types.h>
+#include <sys/cdefs.h>
+
+#define LIDT(idtr) __ASMV("lidt %0" \
+ :: "m" (idtr))
+
+#define IDT_TRAP_GATE_FLAGS 0x8F
+#define IDT_INT_GATE_FLAGS 0x8E
+#define IDT_INT_GATE_USER 0xEE
+
+/*
+ * AMD64 Interrupt Gate Descriptor.
+ */
+struct idt_entry {
+ uint16_t off_lo; /* Low 16 bits of ISR offset */
+ uint16_t segsel; /* Segment selector, hardcode to kernel CS */
+ uint8_t ist : 2; /* Interrupt stack table */
+ uint8_t zero : 1; /* Unused: keep zero */
+ uint8_t zero1 : 4; /* Unused: keep zero */
+ uint8_t type : 4; /* Gate type */
+ uint8_t zero2 : 1; /* Unused: keep zero */
+ uint8_t dpl : 2; /* Descriptor privilege level */
+ uint8_t p : 1; /* Present (keep 1 to mark as valid) */
+ uint16_t off_mid; /* Middle 16 bits of ISR offset */
+ uint32_t off_hi; /* High 32-bits of ISR offset */
+ uint32_t reserved; /* Reserved: keep zero */
+};
+
+struct __packed idtr {
+ uint16_t limit;
+ uintptr_t offset;
+};
+
+
+void idt_set_desc(uint8_t vec, uint8_t type, uintptr_t isr, uint8_t ist);
+void idt_load(void);
+
+#endif /* !_AMD64_IDT_H_ */
diff --git a/sys/include/arch/amd64/trap.h b/sys/include/arch/amd64/trap.h
new file mode 100644
index 0000000..e72ae11
--- /dev/null
+++ b/sys/include/arch/amd64/trap.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2023 Ian Marco Moffett and the VegaOS team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of VegaOS nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* $Id$ */
+
+#ifndef _AMD64_TRAP_H_
+#define _AMD64_TRAP_H_
+
+#if !defined(__ASSEMBLER__)
+#include <sys/types.h>
+#include <machine/frame.h>
+#endif /* !defined(__ASSEMBLER__) */
+
+#define TRAP_BREAKPOINT 0 /* Breakpoint */
+#define TRAP_ARITH_ERR 1 /* Arithmetic error (e.g division by 0) */
+#define TRAP_OVERFLOW 2 /* Overflow */
+#define TRAP_BOUND_RANGE 3 /* BOUND range exceeded */
+#define TRAP_INVLOP 4 /* Invalid opcode */
+#define TRAP_DOUBLE_FAULT 5 /* Double fault */
+#define TRAP_INVLTSS 6 /* Invalid TSS */
+#define TRAP_SEGNP 7 /* Segment not present */
+#define TRAP_PROTFLT 8 /* General protection */
+#define TRAP_PAGEFLT 9 /* Page fault */
+#define TRAP_NMI 10 /* Non-maskable interrupt */
+
+/* Trap is coming from user mode */
+#define TRAP_USER 0x100
+
+#if !defined(__ASSEMBLER__)
+typedef void(*ftrap_handler_t)(void);
+
+void breakpoint_handler(void *sf);
+void arith_err(void *sf);
+void overflow(void *sf);
+void bound_range(void *sf);
+void invl_op(void *sf);
+void double_fault(void *sf);
+void invl_tss(void *sf);
+void segnp(void *sf);
+void general_prot(void *sf);
+void page_fault(void *sf);
+void nmi(void *sf);
+void register_ftrap_handler(ftrap_handler_t handler);
+void trap_handler(struct trapframe *tf);
+#else
+.macro handle_trap
+ mov %rsp, %rdi
+ call trap_handler
+.endm
+#endif /* !defined(__ASSEMBLER__) */
+
+#endif /* !_AMD64_TRAP_H_ */