aboutsummaryrefslogtreecommitdiff
path: root/i686/include
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2023-10-08 19:50:00 +0300
committeraqua <aqua@iserlohn-fortress.net>2023-11-27 21:02:22 +0200
commitfc13fb2ba5993d06d9d6ae5e80bc337aebbdc119 (patch)
tree9607ab4d5f1d4069d4f761a0b25eada36088bd6b /i686/include
parentrules.mk: make include paths absolute (diff)
downloadkernel-fc13fb2ba5993d06d9d6ae5e80bc337aebbdc119.tar.xz
Use meson build system
Diffstat (limited to 'i686/include')
-rw-r--r--i686/include/gdt.h55
-rw-r--r--i686/include/idt.h23
-rw-r--r--i686/include/paging.h59
-rw-r--r--i686/include/sys/control.h24
-rw-r--r--i686/include/sys/cpuid.h35
-rw-r--r--i686/include/sys/io.h92
-rw-r--r--i686/include/sys/syscall.h8
7 files changed, 296 insertions, 0 deletions
diff --git a/i686/include/gdt.h b/i686/include/gdt.h
new file mode 100644
index 0000000..2bdfb22
--- /dev/null
+++ b/i686/include/gdt.h
@@ -0,0 +1,55 @@
+#pragma once
+
+#include <stdbool.h>
+#include <stdint.h>
+
+enum Ring { Ring0 = 0x0, Ring1 = 0x1, Ring2 = 0x2, Ring3 = 0x3 };
+
+struct __attribute__((packed)) Access {
+ unsigned accessed : 1; /* if 0, is set by processor when accessed */
+ unsigned readwrite : 1; /* code seg: read toggle; data seg: write toggle */
+ unsigned direction : 1; /* code seg: conforming bit; data seg: direction bit */
+ unsigned executable : 1; /* executable bit */
+ unsigned segment : 1; /* true for code/data; false for gates/tss */
+ unsigned privilege : 2; /* descriptor privilege level */
+ unsigned present : 1; /* true for every active segment */
+};
+/* _Static_assert(sizeof(struct Access) == 1, "access byte size"); */
+
+static const struct Access null_access = {0, 0, 0, 0, 0, Ring0, 0};
+static const struct Access ktext_access = {0, 1, 0, 1, 1, Ring0, 1};
+static const struct Access kdata_access = {0, 1, 0, 0, 1, Ring0, 1};
+
+/* Segment Descriptor
+ * A memory structure (part of a table) that tells the CPU the attributes of a given segment
+ * |31| | | | | | |24|23|22|21|20|19| | |16|15| | | | | | | 8| 7| | | | | | | 0|
+ * | base_31_24 | G|DB| | A| lim_19_16 | access | base_23_16 |
+ * | base_15_0 | limit_15_0 |
+ * |31| | | | | | | | | | | | | | |16|15| | | | | | | | | | | | | | | 0|
+ * limit size of segment - 1, either in bytes or in 4KiB chunks (check flags)
+ * base address of segment
+ * access
+ * flags defines the segment chunks and 16/32 bit */
+struct __attribute__((packed)) SegmentDescriptor_t {
+ uint16_t limit_15_0; /* low bits of segment limit */
+ uint16_t base_15_0; /* low bits of segment base address */
+ uint8_t base_23_16; /* middle bits of segment base address */
+ uint8_t access; /* access byte */
+ unsigned limit_19_16 : 4; /* high bits of segment limit */
+ /* flags */
+ bool a : 1; /* unused, available for software use */
+ bool rsv : 1; /* reserved */
+ bool db : 1; /* false => 16-bit seg; true => 32-bit seg */
+ bool granularity : 1; /* limit scaled by 4k when set */
+ uint8_t base_31_24; /* high bits of segment address */
+};
+/* _Static_assert(sizeof(struct SegmentDescriptor_t) == 8, "segment descriptor size"); */
+
+void SegmentDescriptor(struct SegmentDescriptor_t *self, unsigned base, unsigned limit, uint8_t access);
+
+void gdt_install();
+
+enum SegmentIndex {
+ ktextDescriptor = 2 * sizeof(struct SegmentDescriptor_t),
+ kdataDescriptor = 3 * sizeof(struct SegmentDescriptor_t)
+};
diff --git a/i686/include/idt.h b/i686/include/idt.h
new file mode 100644
index 0000000..ca39bde
--- /dev/null
+++ b/i686/include/idt.h
@@ -0,0 +1,23 @@
+#pragma once
+
+#include <stdint.h>
+
+struct interrupt_frame {
+ uint32_t ip;
+ uint32_t cs;
+ uint32_t flags;
+ uint32_t sp;
+ uint32_t ss;
+};
+
+/* typedef void (*irq_handler)(); */
+
+/* isr.c */
+void abort_handler(struct interrupt_frame *frame);
+void syscall_handler(struct interrupt_frame *frame);
+void irq0x00(struct interrupt_frame *frame); /* timer interrupt */
+void irq0x01(struct interrupt_frame *frame); /* keyboard interrupt */
+void irq0x0c(struct interrupt_frame *frame); /* mouse interrupt */
+
+/* lidt.c */
+void idt_install();
diff --git a/i686/include/paging.h b/i686/include/paging.h
new file mode 100644
index 0000000..f5bfa78
--- /dev/null
+++ b/i686/include/paging.h
@@ -0,0 +1,59 @@
+#pragma once
+
+/* DirectoryEntry
+ * |31| | | | | | | | | | | | | | | | | | | |11| | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
+ * | page table 4-kb aligned address | avail | G| S| | A| C| W| U| R| P| */
+struct __attribute__((packed)) DirectoryEntry {
+ unsigned present : 1; /* 0: if set, the page is actually in physical memory */
+ unsigned writeable : 1; /* 1: if set, the page is read/write; otherwise the page is read-only */
+ unsigned user : 1; /* 2: if set, then page can be access by all; otherwise only the supervisor can access it */
+ unsigned writethrough : 1; /* 3: if set, write-through caching is enabled; otherwise write-back is enabled instead */
+ unsigned cachedisable : 1; /* 4: if set, the page will not be cached */
+ unsigned accessed : 1; /* 5: set by the CPU when the page is read from or written to */
+ unsigned dirty : 1; /* 6: used to determine whether a page has been written to */
+ unsigned pagesize : 1; /* 7: page size == 0 */
+ unsigned global : 1;
+ unsigned int __available__ : 3; /* available to the OS */
+ unsigned int address : 20;
+};
+/* TODO _Static_assert(sizeof(struct DirectoryEntry) == 4, "DirectoryEntry size"); */
+
+/* DirectoryEntry4MB
+ * |31| | | | | | | | |22|21|20| | | | | | |13|12|11| | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
+ * | bits 31-22 of address |RS| bits 39-22 of |AT| avail | G|PS| D| A|CD|WT|US|RW| P|
+ * | |VD| address */
+struct __attribute__((packed)) DirectoryEntry4MB {
+ unsigned present : 1; /* 0: if set, the page is actually in physical memory */
+ unsigned writeable : 1; /* 1: if set, the page is read/write; otherwise the page is read-only */
+ unsigned useraccess : 1; /* 2: if set, then page can be access by all; otherwise only the supervisor can access it */
+ unsigned writethrough : 1; /* 3: if set, write-through caching is enabled; otherwise write-back is enabled instead */
+ unsigned cachedisable : 1; /* 4: if set, the page will not be cached */
+ unsigned accessed : 1; /* 5: set by the CPU when the page is read from or written to */
+ unsigned dirty : 1; /* 6: used to determine whether a page has been written to */
+ unsigned pagesize : 1; /* 7: page size == 1 */
+ unsigned global : 1; /* 8: */
+ unsigned __available__ : 3; /* 11..9 available to the OS */
+ unsigned pat : 1; /* 12: page attribute table */
+ unsigned int address_high : 8;
+ unsigned rsvd : 1; /* 21 */
+ unsigned int address_low : 10;
+};
+/* TODO _Static_assert(sizeof(struct DirectoryEntry4MB) == 4, "DirectoryEntry4M size"); */
+
+/* TableEntry
+ * |31| | | | | | | | | | | | | | | | | | | |11| | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
+ * | page table 4-kb aligned address | avail | G| | D| A| C| W|US|RW| P| */
+struct __attribute__((packed)) TableEntry {
+ unsigned present : 1; /* if set, the page is actually in physical memory */
+ unsigned writeable : 1; /* if set, the page is read/write; otherwise the page is read-only */
+ unsigned user : 1; /* if set, then page can be access by all; otherwise only the supervisor can access it */
+ unsigned writethrough : 1; /* if set, write-through caching is enabled; otherwise write-back is enabled instead */
+ unsigned cachedisable : 1; /* if set, the page will not be cached */
+ unsigned accessed : 1; /* set by the CPU when the page is read from or written to */
+ unsigned dirty : 1; /* used to determine whether a page has been written to */
+ unsigned pat : 1; /* page attribute table? */
+ unsigned global : 1;
+ unsigned int __available__ : 3; /* available to the OS */
+ unsigned int address : 20;
+};
+/* TODO _Static_assert(sizeof(struct TableEntry) == 4, "TableEntry size"); */
diff --git a/i686/include/sys/control.h b/i686/include/sys/control.h
new file mode 100644
index 0000000..89ab067
--- /dev/null
+++ b/i686/include/sys/control.h
@@ -0,0 +1,24 @@
+#pragma once
+
+static __inline__ void
+abort()
+{
+ /* Symbol h is already defined?
+__asm__(R"(cli
+h: hlt
+jmp h)");
+*/
+ __asm__("cli; hlt");
+}
+
+static __inline__ void
+enable_interrupts()
+{
+ __asm__("sti");
+}
+
+static __inline__ void
+disable_interrupts()
+{
+ __asm__("cli");
+}
diff --git a/i686/include/sys/cpuid.h b/i686/include/sys/cpuid.h
new file mode 100644
index 0000000..6613967
--- /dev/null
+++ b/i686/include/sys/cpuid.h
@@ -0,0 +1,35 @@
+#pragma once
+
+#include <cpuid.h>
+
+struct CPUVersion {
+ unsigned int stepping : 4;
+ unsigned int model : 4;
+ unsigned int family : 4;
+ unsigned int type : 2;
+ unsigned int __unused_1 : 2;
+ unsigned int model_ex : 4;
+ unsigned int family_ex : 8;
+ unsigned int __unused_2 : 4;
+} __attribute__((packed, aligned(__alignof__(unsigned int))));
+/* FIXME _Static_assert(sizeof(struct CPUVersion) == sizeof(unsigned int), "cpuid version struct size"); */
+
+unsigned int
+family(const struct CPUVersion v)
+{
+ if (v.family == 0x0f) return v.family + v.family_ex;
+ else
+ return v.family;
+}
+
+unsigned int
+model(const struct CPUVersion v)
+{
+ switch (v.family) {
+ case 0x06:
+ case 0x0f:
+ return ((unsigned int)v.model_ex << 4) | v.model;
+ default:
+ return v.model;
+ }
+}
diff --git a/i686/include/sys/io.h b/i686/include/sys/io.h
new file mode 100644
index 0000000..4ff5d85
--- /dev/null
+++ b/i686/include/sys/io.h
@@ -0,0 +1,92 @@
+#pragma once
+
+/* port listings */
+enum UART {
+ COM1 = 0x3f8,
+ COM2 = 0x2f8,
+ COM3 = 0x3e8,
+ COM4 = 0x2e8,
+ COM5 = 0x5f8,
+ COM6 = 0x4f8,
+ COM7 = 0x5e8,
+ COM8 = 0x4e8
+};
+
+static __inline__ void
+outb(unsigned char val, unsigned short port)
+{
+ __asm__("outb %0,%1" : : "a"(val), "dN"(port));
+}
+
+static __inline__ void
+outw(unsigned short val, unsigned short port)
+{
+ __asm__("outw %0,%1" : : "a"(val), "dN"(port));
+}
+
+static __inline__ void
+outl(unsigned int val, unsigned short port)
+{
+ __asm__("outl %0,%1" : : "a"(val), "dN"(port));
+}
+
+static __inline__ unsigned char
+inb(unsigned short port)
+{
+ unsigned char val;
+ __asm__("inb %1,%0" : "=a"(val) : "dN"(port));
+ return val;
+}
+
+static __inline__ unsigned short
+inw(unsigned short port)
+{
+ unsigned short val;
+ __asm__("inw %1,%0" : "=a"(val) : "dN"(port));
+ return val;
+}
+
+static __inline__ unsigned int
+inl(unsigned short port)
+{
+ unsigned int val;
+ __asm__("inl %1,%0" : "=a"(val) : "dN"(port));
+ return val;
+}
+
+static __inline__ void
+outsb(unsigned short port, const void *__buf, unsigned long __n)
+{
+ __asm__("cld; rep; outsb" : "+S"(__buf), "+c"(__n) : "d"(port));
+}
+
+static __inline__ void
+outsw(unsigned short port, const void *__buf, unsigned long __n)
+{
+ __asm__("cld; rep; outsw" : "+S"(__buf), "+c"(__n) : "d"(port));
+}
+
+static __inline__ void
+outsl(unsigned short port, const void *__buf, unsigned long __n)
+{
+ __asm__("cld; rep; outsl" : "+S"(__buf), "+c"(__n) : "d"(port));
+}
+
+static __inline__ void
+insb(unsigned short port, void *__buf, unsigned long __n)
+{
+ __asm__("cld; rep; insb" : "+D"(__buf), "+c"(__n) : "d"(port));
+}
+
+static __inline__ void
+insw(unsigned short port, void *__buf, unsigned long __n)
+{
+ __asm__("cld; rep; insw" : "+D"(__buf), "+c"(__n) : "d"(port));
+}
+
+static __inline__ void
+insl(unsigned short port, void *__buf, unsigned long __n)
+{
+ __asm__("cld; rep; insl" : "+D"(__buf), "+c"(__n) : "d"(port));
+}
+
diff --git a/i686/include/sys/syscall.h b/i686/include/sys/syscall.h
new file mode 100644
index 0000000..9e62c89
--- /dev/null
+++ b/i686/include/sys/syscall.h
@@ -0,0 +1,8 @@
+#pragma once
+
+static inline int
+syscall(int number)
+{
+ asm volatile("int $0x80");
+ return 0;
+}